summaryrefslogtreecommitdiff
path: root/clients/tui/nmt-page-ip-tunnel.c
blob: ab58038989a764708bcaa4557a60d634a1636b9d (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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2016 Red Hat, Inc.
 */
/**
 * SECTION:nmt-page-ip_tunnel
 * @short_description: The editor page for IP tunnel connections
 */

#include "libnm/nm-default-client.h"

#include "nmt-page-ip-tunnel.h"

#include "nmt-device-entry.h"
#include "nmt-mtu-entry.h"

G_DEFINE_TYPE(NmtPageIPTunnel, nmt_page_ip_tunnel, NMT_TYPE_EDITOR_PAGE_DEVICE)

#define NMT_PAGE_IP_TUNNEL_GET_PRIVATE(o) \
    (G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_PAGE_IP_TUNNEL, NmtPageIPTunnelPrivate))

typedef struct {
    NmtNewtEntry *input_key;
    NmtNewtEntry *output_key;
} NmtPageIPTunnelPrivate;

NmtEditorPage *
nmt_page_ip_tunnel_new(NMConnection *conn, NmtDeviceEntry *deventry)
{
    return g_object_new(NMT_TYPE_PAGE_IP_TUNNEL,
                        "connection",
                        conn,
                        "device-entry",
                        deventry,
                        NULL);
}

static void
nmt_page_ip_tunnel_init(NmtPageIPTunnel *ip_tunnel)
{}

static void
mode_changed(GObject *object, GParamSpec *pspec, gpointer user_data)
{
    NMSettingIPTunnel *     s_ip_tunnel = NM_SETTING_IP_TUNNEL(object);
    NmtPageIPTunnel *       ip_tunnel   = NMT_PAGE_IP_TUNNEL(user_data);
    NmtPageIPTunnelPrivate *priv        = NMT_PAGE_IP_TUNNEL_GET_PRIVATE(ip_tunnel);
    NMIPTunnelMode          mode;
    gboolean                enable_keys;

    mode        = nm_setting_ip_tunnel_get_mode(s_ip_tunnel);
    enable_keys = NM_IN_SET(mode, NM_IP_TUNNEL_MODE_GRE, NM_IP_TUNNEL_MODE_IP6GRE);
    nmt_newt_widget_set_visible(NMT_NEWT_WIDGET(priv->input_key), enable_keys);
    nmt_newt_widget_set_visible(NMT_NEWT_WIDGET(priv->output_key), enable_keys);

    if (!enable_keys) {
        nmt_newt_entry_set_text(priv->input_key, "");
        nmt_newt_entry_set_text(priv->output_key, "");
    }
}

static NmtNewtPopupEntry tunnel_mode[] = {
    /* The order must match the NM_IP_TUNNEL_MODE_* enum */
    {N_("IPIP"), "IPIP"},
    {N_("GRE"), "GRE"},
    {N_("SIT"), "SIT"},
    {N_("ISATAP"), "ISATAP"},
    {N_("VTI"), "VTI"},
    {N_("IP6IP6"), "IP6IP6"},
    {N_("IPIP6"), "IPIP6"},
    {N_("IP6GRE"), "IP6GRE"},
    {N_("VTI6"), "VTI6"},
    {NULL, NULL}};

static gboolean
add_offset(GBinding *binding, const GValue *from_value, GValue *to_value, gpointer user_data)
{
    guint v;
    int   offset = GPOINTER_TO_INT(user_data);

    g_return_val_if_fail(G_VALUE_HOLDS(from_value, G_TYPE_UINT), FALSE);
    g_return_val_if_fail(G_VALUE_HOLDS(to_value, G_TYPE_UINT), FALSE);

    v = g_value_get_uint(from_value);
    v += offset;

    g_value_set_uint(to_value, v);

    return TRUE;
}

static void
nmt_page_ip_tunnel_constructed(GObject *object)
{
    NmtPageIPTunnel *       ip_tunnel = NMT_PAGE_IP_TUNNEL(object);
    NmtPageIPTunnelPrivate *priv      = NMT_PAGE_IP_TUNNEL_GET_PRIVATE(ip_tunnel);
    NmtEditorSection *      section;
    NmtEditorGrid *         grid;
    NMSettingIPTunnel *     s_ip_tunnel;
    NmtNewtWidget *         widget, *parent;
    NMConnection *          conn;
    GClosure *              s2w, *w2s;

    conn        = nmt_editor_page_get_connection(NMT_EDITOR_PAGE(ip_tunnel));
    s_ip_tunnel = nm_connection_get_setting_ip_tunnel(conn);
    if (!s_ip_tunnel) {
        nm_connection_add_setting(conn, nm_setting_ip_tunnel_new());
        s_ip_tunnel = nm_connection_get_setting_ip_tunnel(conn);
    }

    /* Initialize the mode for new connections */
    if (nm_setting_ip_tunnel_get_mode(s_ip_tunnel) == NM_IP_TUNNEL_MODE_UNKNOWN) {
        g_object_set(s_ip_tunnel, NM_SETTING_IP_TUNNEL_MODE, (guint) NM_IP_TUNNEL_MODE_IPIP, NULL);
    }

    section = nmt_editor_section_new(_("IP tunnel"), NULL, TRUE);
    grid    = nmt_editor_section_get_body(section);

    /* To convert between widget index (0-based) and setting index (1-based) */
    s2w = g_cclosure_new(G_CALLBACK(add_offset), GINT_TO_POINTER(-1), NULL);
    w2s = g_cclosure_new(G_CALLBACK(add_offset), GINT_TO_POINTER(1), NULL);

    widget = nmt_newt_popup_new(tunnel_mode);
    g_object_bind_property_with_closures(s_ip_tunnel,
                                         NM_SETTING_IP_TUNNEL_MODE,
                                         widget,
                                         "active",
                                         G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE,
                                         s2w,
                                         w2s);
    nmt_editor_grid_append(grid, _("Mode"), widget, NULL);

    widget = parent = nmt_device_entry_new(_("Parent"), 40, G_TYPE_NONE);
    g_object_bind_property(s_ip_tunnel,
                           NM_SETTING_IP_TUNNEL_PARENT,
                           widget,
                           "interface-name",
                           G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
    nmt_editor_grid_append(grid, NULL, widget, NULL);

    widget = nmt_newt_entry_new(40, 0);
    nmt_editor_grid_append(grid, _("Local IP"), widget, NULL);
    g_object_bind_property(s_ip_tunnel,
                           NM_SETTING_IP_TUNNEL_LOCAL,
                           widget,
                           "text",
                           G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);

    widget = nmt_newt_entry_new(40, 0);
    nmt_editor_grid_append(grid, _("Remote IP"), widget, NULL);
    g_object_bind_property(s_ip_tunnel,
                           NM_SETTING_IP_TUNNEL_REMOTE,
                           widget,
                           "text",
                           G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);

    widget = nmt_newt_entry_new(40, 0);
    nmt_editor_grid_append(grid, _("Input key"), widget, NULL);
    g_object_bind_property(s_ip_tunnel,
                           NM_SETTING_IP_TUNNEL_INPUT_KEY,
                           widget,
                           "text",
                           G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
    priv->input_key = NMT_NEWT_ENTRY(widget);

    widget = nmt_newt_entry_new(40, 0);
    nmt_editor_grid_append(grid, _("Output key"), widget, NULL);
    g_object_bind_property(s_ip_tunnel,
                           NM_SETTING_IP_TUNNEL_OUTPUT_KEY,
                           widget,
                           "text",
                           G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
    priv->output_key = NMT_NEWT_ENTRY(widget);

    widget = nmt_mtu_entry_new();
    g_object_bind_property(s_ip_tunnel,
                           NM_SETTING_IP_TUNNEL_MTU,
                           widget,
                           "mtu",
                           G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
    nmt_editor_grid_append(grid, _("MTU"), widget, NULL);

    g_signal_connect(s_ip_tunnel,
                     "notify::" NM_SETTING_IP_TUNNEL_MODE,
                     G_CALLBACK(mode_changed),
                     ip_tunnel);
    mode_changed(G_OBJECT(s_ip_tunnel), NULL, ip_tunnel);

    nmt_editor_page_add_section(NMT_EDITOR_PAGE(ip_tunnel), section);

    G_OBJECT_CLASS(nmt_page_ip_tunnel_parent_class)->constructed(object);
}

static void
nmt_page_ip_tunnel_class_init(NmtPageIPTunnelClass *ip_tunnel_class)
{
    GObjectClass *object_class = G_OBJECT_CLASS(ip_tunnel_class);

    g_type_class_add_private(ip_tunnel_class, sizeof(NmtPageIPTunnelPrivate));

    /* virtual methods */
    object_class->constructed = nmt_page_ip_tunnel_constructed;
}