[NetworkManager-openvpn/th/support-proto] all: add support for "proto" flag




commit fc0a4616b8fcc2eafef0605f44cbc2950a78c954
Author: Thomas Haller <thaller redhat com>
Date:   Fri Oct 23 15:31:26 2020 +0200

    all: add support for "proto" flag
    
    Previously, only "proto-tcp" was supported, which could be a boolean
    (anything or "yes").
    
    Obsolete the old flag, and add a "proto" key. If it is present,
    "proto-tcp" is ignored.
    
    The GUI still only has a boolean checkbox and cannot express all the
    options. Editing a profile in the GUI always sets either "udp" or
    "tcp-client".
    
    Obviously, it's ugly to deprecate an existing option, because all the
    code now needs to support the redundant, old option (we will never
    remove deprecated options, so deprecation merely means that it's not
    the best way to do it, not that something will break in the future).
    It's ugly, but still acceptable.

 properties/import-export.c     | 21 ++++++++++++------
 properties/nm-openvpn-editor.c | 14 ++++++++++--
 shared/nm-service-defines.h    |  1 +
 shared/utils.h                 |  7 ++++--
 src/nm-openvpn-service.c       | 49 +++++++++++++++++++++---------------------
 5 files changed, 57 insertions(+), 35 deletions(-)
---
diff --git a/properties/import-export.c b/properties/import-export.c
index a6478d9..29e1541 100644
--- a/properties/import-export.c
+++ b/properties/import-export.c
@@ -924,9 +924,16 @@ do_import (const char *path, const char *contents, gsize contents_len, GError **
                                line_error = g_strdup_printf (_("proto expects protocol type like “udp” or 
“tcp”"));
                                goto handle_line_error;
                        }
-                       if (!NM_IN_STRSET (params[1], "udp", "udp4", "udp6")) {
-                               setting_vpn_add_data_item (s_vpn, NM_OPENVPN_KEY_PROTO_TCP, "yes");
-                       }
+                       setting_vpn_add_data_item_or_remove (s_vpn,
+                                                            NM_OPENVPN_KEY_PROTO,
+                                                              NM_IN_STRSET (params[1], "udp")
+                                                            ? NULL
+                                                            : params[1]);
+                       setting_vpn_add_data_item_or_remove (s_vpn,
+                                                            NM_OPENVPN_KEY_PROTO_TCP,
+                                                              NM_IN_STRSET (params[1], 
NMOVPN_PROTCOL_TYPES_UDP)
+                                                            ? NULL
+                                                            : "yes");
                        continue;
                }
 
@@ -2111,10 +2118,10 @@ do_export_create (NMConnection *connection, const char *path, GError **error)
                        args_write_line (f, NMV_OVPN_TAG_DEV_TYPE, device_type);
        }
 
-       args_write_line (f,
-                        NMV_OVPN_TAG_PROTO,
-                        nm_streq0 (nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_PROTO_TCP), "yes")
-                            ? "tcp" : "udp");
+       value = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_PROTO);
+       if (!value)
+               value = nm_streq0 (nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_PROTO_TCP), "yes") ? 
"tcp" : "udp";
+       args_write_line (f, NMV_OVPN_TAG_PROTO, value);
 
        args_write_line_setting_value_int (f, NMV_OVPN_TAG_PORT, s_vpn, NM_OPENVPN_KEY_PORT);
 
diff --git a/properties/nm-openvpn-editor.c b/properties/nm-openvpn-editor.c
index 2d0f204..dc34b04 100644
--- a/properties/nm-openvpn-editor.c
+++ b/properties/nm-openvpn-editor.c
@@ -670,6 +670,7 @@ static const char *const advanced_keys[] = {
        NM_OPENVPN_KEY_PING_EXIT,
        NM_OPENVPN_KEY_PING_RESTART,
        NM_OPENVPN_KEY_PORT,
+       NM_OPENVPN_KEY_PROTO,
        NM_OPENVPN_KEY_PROTO_TCP,
        NM_OPENVPN_KEY_PROXY_PORT,
        NM_OPENVPN_KEY_PROXY_RETRY,
@@ -1433,6 +1434,7 @@ advanced_dialog_new (GHashTable *hash, const char *contype)
        GtkListStore *store;
        GtkTreeIter iter;
        int vint;
+       gboolean vbool;
        guint32 active;
        NMSettingSecretFlags pw_flags;
        GError *error = NULL;
@@ -1565,9 +1567,15 @@ advanced_dialog_new (GHashTable *hash, const char *contype)
 
        _builder_init_toggle_button (builder, "mssfix_checkbutton", _hash_get_boolean (hash, 
NM_OPENVPN_KEY_MSSFIX));
        _builder_init_toggle_button (builder, "float_checkbutton", _hash_get_boolean (hash, 
NM_OPENVPN_KEY_FLOAT));
-       _builder_init_toggle_button (builder, "tcp_checkbutton", _hash_get_boolean (hash, 
NM_OPENVPN_KEY_PROTO_TCP));
        _builder_init_toggle_button (builder, "ncp_disable_checkbutton", _hash_get_boolean (hash, 
NM_OPENVPN_KEY_NCP_DISABLE));
 
+       value = g_hash_table_lookup (hash, NM_OPENVPN_KEY_PROTO);
+       if (value)
+               vbool = !NM_IN_STRSET (value, NMOVPN_PROTCOL_TYPES_UDP);
+       else
+               vbool = _hash_get_boolean (hash, NM_OPENVPN_KEY_PROTO_TCP);
+       _builder_init_toggle_button (builder, "tcp_checkbutton", vbool);
+
        /* Populate device-related widgets */
        dev =      g_hash_table_lookup (hash, NM_OPENVPN_KEY_DEV);
        dev_type = g_hash_table_lookup (hash, NM_OPENVPN_KEY_DEV_TYPE);
@@ -1932,8 +1940,10 @@ advanced_dialog_new_hash_from_dialog (GtkWidget *dialog)
                g_hash_table_insert (hash, NM_OPENVPN_KEY_FLOAT, g_strdup ("yes"));
 
        widget = GTK_WIDGET (gtk_builder_get_object (builder, "tcp_checkbutton"));
-       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
+       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) {
+               g_hash_table_insert (hash, NM_OPENVPN_KEY_PROTO, g_strdup ("tcp-client"));
                g_hash_table_insert (hash, NM_OPENVPN_KEY_PROTO_TCP, g_strdup ("yes"));
+       }
 
        widget = GTK_WIDGET (gtk_builder_get_object (builder, "ncp_disable_checkbutton"));
        if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
diff --git a/shared/nm-service-defines.h b/shared/nm-service-defines.h
index 46b4104..6e20d99 100644
--- a/shared/nm-service-defines.h
+++ b/shared/nm-service-defines.h
@@ -58,6 +58,7 @@
 #define NM_OPENVPN_KEY_PING_EXIT                 "ping-exit"
 #define NM_OPENVPN_KEY_PING_RESTART              "ping-restart"
 #define NM_OPENVPN_KEY_PORT                      "port"
+#define NM_OPENVPN_KEY_PROTO                     "proto"
 #define NM_OPENVPN_KEY_PROTO_TCP                 "proto-tcp"
 #define NM_OPENVPN_KEY_PROXY_PORT                "proxy-port"
 #define NM_OPENVPN_KEY_PROXY_RETRY               "proxy-retry"
diff --git a/shared/utils.h b/shared/utils.h
index 477ad70..388da12 100644
--- a/shared/utils.h
+++ b/shared/utils.h
@@ -102,10 +102,13 @@ gboolean is_pkcs12 (const char *filepath);
 
 gboolean is_encrypted (const char *filename);
 
-#define NMOVPN_PROTCOL_TYPES \
+#define NMOVPN_PROTCOL_TYPES_UDP \
        "udp", \
        "udp4", \
-       "udp6", \
+       "udp6"
+
+#define NMOVPN_PROTCOL_TYPES \
+       NMOVPN_PROTCOL_TYPES_UDP, \
        "tcp", \
        "tcp4", \
        "tcp6", \
diff --git a/src/nm-openvpn-service.c b/src/nm-openvpn-service.c
index e9fa084..e5aa911 100644
--- a/src/nm-openvpn-service.c
+++ b/src/nm-openvpn-service.c
@@ -162,6 +162,7 @@ static const ValidProperty valid_properties[] = {
        { NM_OPENVPN_KEY_PING_EXIT,                 G_TYPE_INT, 0, G_MAXINT, FALSE },
        { NM_OPENVPN_KEY_PING_RESTART,              G_TYPE_INT, 0, G_MAXINT, FALSE },
        { NM_OPENVPN_KEY_MAX_ROUTES,                G_TYPE_INT, 0, 100000000, FALSE },
+       { NM_OPENVPN_KEY_PROTO,                     G_TYPE_STRING, 0, 0, FALSE },
        { NM_OPENVPN_KEY_PROTO_TCP,                 G_TYPE_BOOLEAN, 0, 0, FALSE },
        { NM_OPENVPN_KEY_PORT,                      G_TYPE_INT, 1, 65535, FALSE },
        { NM_OPENVPN_KEY_PROXY_TYPE,                G_TYPE_STRING, 0, 0, FALSE },
@@ -1322,7 +1323,8 @@ nm_openvpn_start_openvpn_binary (NMOpenvpnPlugin *plugin,
        gs_unref_ptrarray GPtrArray *args = NULL;
        GPid pid;
        gboolean dev_type_is_tap;
-       const char *defport, *proto_tcp;
+       const char *defport;
+       const char *proto_global;
        const char *compress;
        const char *tls_remote = NULL;
        const char *nm_openvpn_user, *nm_openvpn_group, *nm_openvpn_chroot;
@@ -1379,9 +1381,14 @@ nm_openvpn_start_openvpn_binary (NMOpenvpnPlugin *plugin,
        if (!nmovpn_arg_is_set (defport))
                defport = NULL;
 
-       proto_tcp = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_PROTO_TCP);
-       if (!nmovpn_arg_is_set (proto_tcp))
-               proto_tcp = NULL;
+       proto_global = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_PROTO);
+       if (!proto_global) {
+               proto_global = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_PROTO_TCP);
+               if (nm_streq0 (proto_global, "yes"))
+                       proto_global = "tcp-client";
+               else
+                       proto_global = "udp";
+       }
 
        tmp = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_REMOTE);
        if (tmp && *tmp) {
@@ -1392,7 +1399,9 @@ nm_openvpn_start_openvpn_binary (NMOpenvpnPlugin *plugin,
                tmp_remaining = tmp_clone = g_strdup (tmp);
                while ((tok = strsep (&tmp_remaining, " \t,")) != NULL) {
                        gs_free char *str_free = NULL;
-                       const char *host, *port, *proto;
+                       const char *host;
+                       const char *port;
+                       const char *proto;
                        gssize eidx;
 
                        eidx = nmovpn_remote_parse (tok,
@@ -1422,27 +1431,19 @@ nm_openvpn_start_openvpn_binary (NMOpenvpnPlugin *plugin,
                        } else
                                args_add_strv (args, "1194"); /* default IANA port */
 
-                       if (proto) {
-                               if (nm_streq (proto, "tcp"))
-                                       args_add_strv (args, "tcp-client");
-                               else if (nm_streq (proto, "tcp4"))
-                                       args_add_strv (args, "tcp4-client");
-                               else if (nm_streq (proto, "tcp6"))
-                                       args_add_strv (args, "tcp6-client");
-                               else if (NM_IN_STRSET (proto, NMOVPN_PROTCOL_TYPES))
-                                       args_add_strv (args, proto);
-                               else {
-                                       g_set_error (error,
-                                                    NM_VPN_PLUGIN_ERROR,
-                                                    NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
-                                                    _("Invalid proto “%s”."), proto);
-                                       return FALSE;
-                               }
-                       } else if (nm_streq0 (proto_tcp, "yes"))
+                       if (!proto)
+                               proto = proto_global;
+
+                       if (nm_streq (proto, "tcp"))
                                args_add_strv (args, "tcp-client");
+                       else if (nm_streq (proto, "tcp4"))
+                               args_add_strv (args, "tcp4-client");
+                       else if (nm_streq (proto, "tcp6"))
+                               args_add_strv (args, "tcp6-client");
                        else {
-                               args_add_strv (args, "udp");
-                               args_add_strv (args, "--explicit-exit-notify");
+                               args_add_strv (args, proto);
+                               if (NM_IN_STRSET (proto, NMOVPN_PROTCOL_TYPES_UDP))
+                                       args_add_strv (args, "--explicit-exit-notify");
                        }
                }
        }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]