[network-manager-pptp/th/logging-bgo771664: 1/11] shared: update shared files



commit 706fd54160410ac6d272f6f6b60f7d87a7141a6c
Author: Thomas Haller <thaller redhat com>
Date:   Mon Sep 19 15:59:59 2016 +0200

    shared: update shared files

 po/POTFILES.in                         |    1 +
 shared/Makefile.am                     |    3 +
 shared/nm-utils/nm-glib.h              |   63 ++++++++-
 shared/nm-utils/nm-macros-internal.h   |  115 +++++++++++++++
 shared/nm-utils/nm-shared-utils.c      |  239 ++++++++++++++++++++++++++++++++
 shared/nm-utils/nm-shared-utils.h      |   66 +++++++++
 shared/nm-utils/nm-vpn-plugin-macros.h |   93 ++++++++++++
 7 files changed, 576 insertions(+), 4 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 388fc33..1a8e283 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -5,6 +5,7 @@ auth-dialog/main.c
 properties/advanced-dialog.c
 properties/nm-pptp-editor-plugin.c
 properties/nm-pptp-editor.c
+shared/nm-utils/nm-shared-utils.c
 shared/nm-utils/nm-vpn-plugin-utils.c
 src/nm-pptp-service.c
 [type: gettext/glade]properties/nm-pptp-dialog.ui
diff --git a/shared/Makefile.am b/shared/Makefile.am
index a72f8ce..e94c9bc 100644
--- a/shared/Makefile.am
+++ b/shared/Makefile.am
@@ -3,6 +3,9 @@ EXTRA_DIST = \
     nm-utils/gsystem-local-alloc.h \
     nm-utils/nm-glib.h \
     nm-utils/nm-macros-internal.h \
+    nm-utils/nm-shared-utils.c \
+    nm-utils/nm-shared-utils.h \
+    nm-utils/nm-vpn-plugin-macros.h \
     nm-utils/nm-vpn-plugin-utils.c \
     nm-utils/nm-vpn-plugin-utils.h \
     nm-service-defines.h \
diff --git a/shared/nm-utils/nm-glib.h b/shared/nm-utils/nm-glib.h
index 46a8bb2..824a08c 100644
--- a/shared/nm-utils/nm-glib.h
+++ b/shared/nm-utils/nm-glib.h
@@ -313,14 +313,14 @@ _g_key_file_save_to_file (GKeyFile     *key_file,
 
 #if GLIB_CHECK_VERSION (2, 36, 0)
 #define g_credentials_get_unix_pid(creds, error) \
-       G_GNUC_EXTENSION ({ \
+       ({ \
                G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
                        (g_credentials_get_unix_pid) ((creds), (error)); \
                G_GNUC_END_IGNORE_DEPRECATIONS \
        })
 #else
 #define g_credentials_get_unix_pid(creds, error) \
-       G_GNUC_EXTENSION ({ \
+       ({ \
                struct ucred *native_creds; \
                 \
                native_creds = g_credentials_get_native ((creds), G_CREDENTIALS_TYPE_LINUX_UCRED); \
@@ -357,12 +357,12 @@ _nm_g_hash_table_get_keys_as_array (GHashTable *hash_table,
 #endif
 #if !GLIB_CHECK_VERSION(2, 40, 0)
 #define g_hash_table_get_keys_as_array(hash_table, length) \
-       G_GNUC_EXTENSION ({ \
+       ({ \
                _nm_g_hash_table_get_keys_as_array (hash_table, length); \
        })
 #else
 #define g_hash_table_get_keys_as_array(hash_table, length) \
-       G_GNUC_EXTENSION ({ \
+       ({ \
                G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
                        (g_hash_table_get_keys_as_array) ((hash_table), (length)); \
                G_GNUC_END_IGNORE_DEPRECATIONS \
@@ -394,4 +394,59 @@ g_steal_pointer (gpointer pp)
   (0 ? (*(pp)) : (g_steal_pointer) (pp))
 #endif
 
+
+static inline gboolean
+_nm_g_strv_contains (const gchar * const *strv,
+                     const gchar         *str)
+{
+#if !GLIB_CHECK_VERSION(2, 44, 0)
+       g_return_val_if_fail (strv != NULL, FALSE);
+       g_return_val_if_fail (str != NULL, FALSE);
+
+       for (; *strv != NULL; strv++) {
+               if (g_str_equal (str, *strv))
+                       return TRUE;
+       }
+
+       return FALSE;
+#else
+       G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+       return g_strv_contains (strv, str);
+       G_GNUC_END_IGNORE_DEPRECATIONS
+#endif
+}
+#define g_strv_contains _nm_g_strv_contains
+
+static inline GVariant *
+_nm_g_variant_new_take_string (gchar *string)
+{
+#if !GLIB_CHECK_VERSION(2, 36, 0)
+       GVariant *value;
+
+       g_return_val_if_fail (string != NULL, NULL);
+       g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL);
+
+       value = g_variant_new_string (string);
+       g_free (string);
+       return value;
+#elif !GLIB_CHECK_VERSION(2, 38, 0)
+       GVariant *value;
+       GBytes *bytes;
+
+       g_return_val_if_fail (string != NULL, NULL);
+       g_return_val_if_fail (g_utf8_validate (string, -1, NULL), NULL);
+
+       bytes = g_bytes_new_take (string, strlen (string) + 1);
+       value = g_variant_new_from_bytes (G_VARIANT_TYPE_STRING, bytes, TRUE);
+       g_bytes_unref (bytes);
+
+       return value;
+#else
+       G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+       return g_variant_new_take_string (string);
+       G_GNUC_END_IGNORE_DEPRECATIONS
+#endif
+}
+#define g_variant_new_take_string _nm_g_variant_new_take_string
+
 #endif  /* __NM_GLIB_H__ */
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index 603689a..91970c3 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -51,6 +51,14 @@ _nm_auto_unset_gvalue_impl (GValue *v)
 }
 #define nm_auto_unset_gvalue nm_auto(_nm_auto_unset_gvalue_impl)
 
+static inline void
+_nm_auto_free_gstring_impl (GString **str)
+{
+       if (*str)
+               g_string_free (*str, TRUE);
+}
+#define nm_auto_free_gstring nm_auto(_nm_auto_free_gstring_impl)
+
 /********************************************************/
 
 /* http://stackoverflow.com/a/11172679 */
@@ -294,6 +302,22 @@ _NM_IN_STRSET_streq (const char *x, const char *s)
 
 /*****************************************************************************/
 
+#define nm_str_not_empty(str) \
+       ({ \
+               /* implemented as macro to preserve constness */ \
+               typeof (str) __str = (str); \
+               _nm_unused const char *__str_type_check = __str; \
+               ((__str && __str[0]) ? __str : ((char *) NULL)); \
+       })
+
+static inline char *
+nm_strdup_not_empty (const char *str)
+{
+       return str && str[0] ? g_strdup (str) : NULL;
+}
+
+/*****************************************************************************/
+
 #define NM_PRINT_FMT_QUOTED(cond, prefix, str, suffix, str_else) \
        (cond) ? (prefix) : "", \
        (cond) ? (str) : (str_else), \
@@ -348,6 +372,24 @@ _notify (obj_type *obj, _PropertyEnums prop) \
 
 /*****************************************************************************/
 
+#define __NM_GET_PRIVATE(self, type, is_check, result_cmd) \
+       ({ \
+               /* preserve the const-ness of self. Unfortunately, that
+                * way, @self cannot be a void pointer */ \
+               typeof (self) _self = (self); \
+               \
+               /* Get compiler error if variable is of wrong type */ \
+               _nm_unused const type *_self2 = (_self); \
+               \
+               nm_assert (is_check (_self)); \
+               ( result_cmd ); \
+       })
+
+#define _NM_GET_PRIVATE(self, type, is_check)     __NM_GET_PRIVATE(self, type, is_check, &_self->_priv)
+#define _NM_GET_PRIVATE_PTR(self, type, is_check) __NM_GET_PRIVATE(self, type, is_check,  _self->_priv)
+
+/*****************************************************************************/
+
 static inline gpointer
 nm_g_object_ref (gpointer obj)
 {
@@ -518,6 +560,50 @@ nm_strcmp_p_with_data (gconstpointer a, gconstpointer b, gpointer user_data)
 
 /*****************************************************************************/
 
+/* Taken from systemd's UNIQ_T and UNIQ macros. */
+
+#define NM_UNIQ_T(x, uniq) G_PASTE(__unique_prefix_, G_PASTE(x, uniq))
+#define NM_UNIQ __COUNTER__
+
+/*****************************************************************************/
+
+/* glib's MIN()/MAX() macros don't have function-like behavior, in that they evaluate
+ * the argument possibly twice.
+ *
+ * Taken from systemd's MIN()/MAX() macros. */
+
+#define NM_MIN(a, b) __NM_MIN(NM_UNIQ, a, NM_UNIQ, b)
+#define __NM_MIN(aq, a, bq, b) \
+       ({ \
+               typeof (a) NM_UNIQ_T(A, aq) = (a); \
+               typeof (b) NM_UNIQ_T(B, bq) = (b); \
+               ((NM_UNIQ_T(A, aq) < NM_UNIQ_T(B, bq)) ? NM_UNIQ_T(A, aq) : NM_UNIQ_T(B, bq)); \
+       })
+
+#define NM_MAX(a, b) __NM_MAX(NM_UNIQ, a, NM_UNIQ, b)
+#define __NM_MAX(aq, a, bq, b) \
+       ({ \
+               typeof (a) NM_UNIQ_T(A, aq) = (a); \
+               typeof (b) NM_UNIQ_T(B, bq) = (b); \
+               ((NM_UNIQ_T(A, aq) > NM_UNIQ_T(B, bq)) ? NM_UNIQ_T(A, aq) : NM_UNIQ_T(B, bq)); \
+       })
+
+#define NM_CLAMP(x, low, high) __NM_CLAMP(NM_UNIQ, x, NM_UNIQ, low, NM_UNIQ, high)
+#define __NM_CLAMP(xq, x, lowq, low, highq, high) \
+       ({ \
+               typeof(x)NM_UNIQ_T(X,xq) = (x); \
+               typeof(low) NM_UNIQ_T(LOW,lowq) = (low); \
+               typeof(high) NM_UNIQ_T(HIGH,highq) = (high); \
+               \
+               ( (NM_UNIQ_T(X,xq) > NM_UNIQ_T(HIGH,highq)) \
+                 ? NM_UNIQ_T(HIGH,highq) \
+                 : (NM_UNIQ_T(X,xq) < NM_UNIQ_T(LOW,lowq)) \
+                    ? NM_UNIQ_T(LOW,lowq) \
+                    : NM_UNIQ_T(X,xq)); \
+       })
+
+/*****************************************************************************/
+
 static inline guint
 nm_encode_version (guint major, guint minor, guint micro) {
        /* analog to the preprocessor macro NM_ENCODE_VERSION(). */
@@ -532,6 +618,35 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
 }
 /*****************************************************************************/
 
+/* if @str is NULL, return "(null)". Otherwise, allocate a buffer using
+ * alloca() of size @bufsize and fill it with @str. @str will be quoted with
+ * single quote, and in case @str is too long, the final quote will be '^'. */
+#define nm_strquote_a(bufsize, str) \
+       ({ \
+               G_STATIC_ASSERT ((bufsize) >= 6); \
+               const gsize _BUFSIZE = (bufsize); \
+               const char *const _s = (str); \
+               char *_r; \
+               gsize _l; \
+               gboolean _truncated; \
+               \
+               nm_assert (_BUFSIZE >= 6); \
+               \
+               if (_s) { \
+                       _l = strlen (_s) + 3; \
+                       if ((_truncated = (_BUFSIZE < _l))) \
+                               _l = _BUFSIZE; \
+                       \
+                       _r = g_alloca (_l); \
+                       _r[0] = '\''; \
+                       memcpy (&_r[1], _s, _l - 3); \
+                       _r[_l - 2] = _truncated ? '^' : '\''; \
+                       _r[_l - 1] = '\0'; \
+               } else \
+                       _r = "(null)"; \
+               _r; \
+       })
+
 #define nm_sprintf_buf(buf, format, ...) ({ \
                char * _buf = (buf); \
                \
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c
new file mode 100644
index 0000000..38f6529
--- /dev/null
+++ b/shared/nm-utils/nm-shared-utils.c
@@ -0,0 +1,239 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
+/* NetworkManager -- Network link manager
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ *
+ * (C) Copyright 2016 Red Hat, Inc.
+ */
+
+#include "nm-default.h"
+
+#include "nm-shared-utils.h"
+
+#include <errno.h>
+
+/*****************************************************************************/
+
+/* _nm_utils_ascii_str_to_int64:
+ *
+ * A wrapper for g_ascii_strtoll, that checks whether the whole string
+ * can be successfully converted to a number and is within a given
+ * range. On any error, @fallback will be returned and %errno will be set
+ * to a non-zero value. On success, %errno will be set to zero, check %errno
+ * for errors. Any trailing or leading (ascii) white space is ignored and the
+ * functions is locale independent.
+ *
+ * The function is guaranteed to return a value between @min and @max
+ * (inclusive) or @fallback. Also, the parsing is rather strict, it does
+ * not allow for any unrecognized characters, except leading and trailing
+ * white space.
+ **/
+gint64
+_nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback)
+{
+       gint64 v;
+       char *s = NULL;
+
+       if (str) {
+               while (g_ascii_isspace (str[0]))
+                       str++;
+       }
+       if (!str || !str[0]) {
+               errno = EINVAL;
+               return fallback;
+       }
+
+       errno = 0;
+       v = g_ascii_strtoll (str, &s, base);
+
+       if (errno != 0)
+               return fallback;
+       if (s[0] != '\0') {
+               while (g_ascii_isspace (s[0]))
+                       s++;
+               if (s[0] != '\0') {
+                       errno = EINVAL;
+                       return fallback;
+               }
+       }
+       if (v > max || v < min) {
+               errno = ERANGE;
+               return fallback;
+       }
+
+       return v;
+}
+
+/*****************************************************************************/
+
+gint
+_nm_utils_ascii_str_to_bool (const char *str,
+                             gint default_value)
+{
+       gsize len;
+       char *s = NULL;
+
+       if (!str)
+               return default_value;
+
+       while (str[0] && g_ascii_isspace (str[0]))
+               str++;
+
+       if (!str[0])
+               return default_value;
+
+       len = strlen (str);
+       if (g_ascii_isspace (str[len - 1])) {
+               s = g_strdup (str);
+               g_strchomp (s);
+               str = s;
+       }
+
+       if (!g_ascii_strcasecmp (str, "true") || !g_ascii_strcasecmp (str, "yes") || !g_ascii_strcasecmp 
(str, "on") || !g_ascii_strcasecmp (str, "1"))
+               default_value = TRUE;
+       else if (!g_ascii_strcasecmp (str, "false") || !g_ascii_strcasecmp (str, "no") || !g_ascii_strcasecmp 
(str, "off") || !g_ascii_strcasecmp (str, "0"))
+               default_value = FALSE;
+       if (s)
+               g_free (s);
+       return default_value;
+}
+
+/*****************************************************************************/
+
+G_DEFINE_QUARK (nm-utils-error-quark, nm_utils_error)
+
+void
+nm_utils_error_set_cancelled (GError **error,
+                              gboolean is_disposing,
+                              const char *instance_name)
+{
+       if (is_disposing) {
+               g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_CANCELLED_DISPOSING,
+                            "Disposing %s instance",
+                            instance_name && *instance_name ? instance_name : "source");
+       } else {
+               g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CANCELLED,
+                                    "Request cancelled");
+       }
+}
+
+gboolean
+nm_utils_error_is_cancelled (GError *error,
+                             gboolean consider_is_disposing)
+{
+       if (error) {
+               if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+                       return TRUE;
+               if (   consider_is_disposing
+                   && g_error_matches (error, NM_UTILS_ERROR, NM_UTILS_ERROR_CANCELLED_DISPOSING))
+                       return TRUE;
+       }
+       return FALSE;
+}
+
+/*****************************************************************************/
+
+/**
+ * nm_g_object_set_property:
+ * @object: the target object
+ * @property_name: the property name
+ * @value: the #GValue to set
+ * @error: (allow-none): optional error argument
+ *
+ * A reimplementation of g_object_set_property(), but instead
+ * returning an error instead of logging a warning. All g_object_set*()
+ * versions in glib require you to not pass invalid types or they will
+ * log a g_warning() -- without reporting an error. We don't want that,
+ * so we need to hack error checking around it.
+ *
+ * Returns: whether the value was successfully set.
+ */
+gboolean
+nm_g_object_set_property (GObject *object,
+                          const gchar  *property_name,
+                          const GValue *value,
+                          GError **error)
+{
+       GParamSpec *pspec;
+       nm_auto_unset_gvalue GValue tmp_value = G_VALUE_INIT;
+       GObjectClass *klass;
+
+       g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
+       g_return_val_if_fail (property_name != NULL, FALSE);
+       g_return_val_if_fail (G_IS_VALUE (value), FALSE);
+       g_return_val_if_fail (!error || !*error, FALSE);
+
+       /* g_object_class_find_property() does g_param_spec_get_redirect_target(),
+        * where we differ from a plain g_object_set_property(). */
+       pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property_name);
+
+       if (!pspec) {
+               g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+                            _("object class '%s' has no property named '%s'"),
+                            G_OBJECT_TYPE_NAME (object),
+                            property_name);
+               return FALSE;
+       }
+       if (!(pspec->flags & G_PARAM_WRITABLE)) {
+               g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+                            _("property '%s' of object class '%s' is not writable"),
+                            pspec->name,
+                            G_OBJECT_TYPE_NAME (object));
+               return FALSE;
+       }
+       if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY)) {
+               g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+                            _("construct property \"%s\" for object '%s' can't be set after construction"),
+                            pspec->name, G_OBJECT_TYPE_NAME (object));
+               return FALSE;
+       }
+
+       klass = g_type_class_peek (pspec->owner_type);
+       if (klass == NULL) {
+               g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+                            _("'%s::%s' is not a valid property name; '%s' is not a GObject subtype"),
+                           g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
+               return FALSE;
+       }
+
+       /* provide a copy to work from, convert (if necessary) and validate */
+       g_value_init (&tmp_value, pspec->value_type);
+       if (!g_value_transform (value, &tmp_value)) {
+               g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+                            _("unable to set property '%s' of type '%s' from value of type '%s'"),
+                            pspec->name,
+                            g_type_name (pspec->value_type),
+                            G_VALUE_TYPE_NAME (value));
+               return FALSE;
+       }
+       if (   g_param_value_validate (pspec, &tmp_value)
+           && !(pspec->flags & G_PARAM_LAX_VALIDATION)) {
+               gs_free char *contents = g_strdup_value_contents (value);
+
+               g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_UNKNOWN,
+                            _("value \"%s\" of type '%s' is invalid or out of range for property '%s' of 
type '%s'"),
+                            contents,
+                            G_VALUE_TYPE_NAME (value),
+                            pspec->name,
+                            g_type_name (pspec->value_type));
+               return FALSE;
+       }
+
+       g_object_set_property (object, property_name, &tmp_value);
+       return TRUE;
+}
+
+/*****************************************************************************/
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h
new file mode 100644
index 0000000..cfa8f99
--- /dev/null
+++ b/shared/nm-utils/nm-shared-utils.h
@@ -0,0 +1,66 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
+/* NetworkManager -- Network link manager
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ *
+ * (C) Copyright 2016 Red Hat, Inc.
+ */
+
+#ifndef __NM_SHARED_UTILS_H__
+#define __NM_SHARED_UTILS_H__
+
+/******************************************************************************/
+
+gint64 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback);
+
+gint _nm_utils_ascii_str_to_bool (const char *str,
+                                  gint default_value);
+
+/******************************************************************************/
+
+/**
+ * NMUtilsError:
+ * @NM_UTILS_ERROR_UNKNOWN: unknown or unclassified error
+ * @NM_UTILS_ERROR_CANCELLED_DISPOSING: when disposing an object that has
+ *   pending aynchronous operations, the operation is cancelled with this
+ *   error reason. Depending on the usage, this might indicate a bug because
+ *   usually the target object should stay alive as long as there are pending
+ *   operations.
+ */
+typedef enum {
+       NM_UTILS_ERROR_UNKNOWN = 0,                 /*< nick=Unknown >*/
+       NM_UTILS_ERROR_CANCELLED_DISPOSING,         /*< nick=CancelledDisposing >*/
+} NMUtilsError;
+
+#define NM_UTILS_ERROR (nm_utils_error_quark ())
+GQuark nm_utils_error_quark (void);
+
+void nm_utils_error_set_cancelled (GError **error,
+                                   gboolean is_disposing,
+                                   const char *instance_name);
+gboolean nm_utils_error_is_cancelled (GError *error,
+                                      gboolean consider_is_disposing);
+
+/******************************************************************************/
+
+gboolean nm_g_object_set_property (GObject *object,
+                                   const gchar  *property_name,
+                                   const GValue *value,
+                                   GError **error);
+
+/******************************************************************************/
+
+#endif /* __NM_SHARED_UTILS_H__ */
diff --git a/shared/nm-utils/nm-vpn-plugin-macros.h b/shared/nm-utils/nm-vpn-plugin-macros.h
new file mode 100644
index 0000000..acc549f
--- /dev/null
+++ b/shared/nm-utils/nm-vpn-plugin-macros.h
@@ -0,0 +1,93 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
+
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ *
+ * Copyright 2016 Red Hat, Inc.
+ */
+
+#ifndef __NM_VPN_PLUGIN_MACROS_H__
+#define __NM_VPN_PLUGIN_MACROS_H__
+
+#include <syslog.h>
+
+static inline int
+nm_utils_syslog_coerce_from_nm (int syslog_level)
+{
+       /* NetworkManager uses internally NMLogLevel levels. When spawning
+        * the VPN plugin, it maps those levels to syslog levels as follows:
+        *
+        *  LOGL_INFO = LOG_NOTICE,
+        *  LOGL_DEBUG = LOG_INFO,
+        *  LOGL_TRACE = LOG_DEBUG,
+        *
+        * However, when actually printing to syslog, we don't want to print messages
+        * with LOGL_INFO level as LOG_NOTICE, because they are *not* to be highlighted.
+        *
+        * In other words: NetworkManager has 3 levels that should not require highlighting:
+        * LOGL_INFO, LOGL_DEBUG, LOGL_TRACE. syslog on the other hand has only LOG_INFO and LOG_DEBUG.
+        *
+        * So, coerce those values before printing to syslog. When you receive the syslog_level
+        * from NetworkManager, instead of calling
+        *   syslog(syslog_level, ...)
+        * you should call
+        *   syslog(nm_utils_syslog_coerce_from_nm(syslog_level), ...)
+        */
+       switch (syslog_level) {
+       case LOG_INFO:
+               return LOG_DEBUG;
+       case LOG_NOTICE:
+               return LOG_INFO;
+       default:
+               return syslog_level;
+       }
+}
+
+static inline const char *
+nm_utils_syslog_to_str (int syslog_level)
+{
+       /* Maps the levels the same way as NetworkManager's nm-logging.c does */
+       if (syslog_level >= LOG_DEBUG)
+               return "<trace>";
+       if (syslog_level >= LOG_INFO)
+               return "<debug>";
+       if (syslog_level >= LOG_NOTICE)
+               return "<info>";
+       if (syslog_level >= LOG_WARNING)
+               return "<warn>";
+       return "<error>";
+}
+
+/*****************************************************************************/
+
+/* possibly missing defines from newer libnm API. */
+
+#ifndef NM_VPN_PLUGIN_CONFIG_PROXY_PAC
+#define NM_VPN_PLUGIN_CONFIG_PROXY_PAC "pac"
+#endif
+
+#ifndef NM_VPN_PLUGIN_IP4_CONFIG_PRESERVE_ROUTES
+#define NM_VPN_PLUGIN_IP4_CONFIG_PRESERVE_ROUTES "preserve-routes"
+#endif
+
+#ifndef NM_VPN_PLUGIN_IP6_CONFIG_PRESERVE_ROUTES
+#define NM_VPN_PLUGIN_IP6_CONFIG_PRESERVE_ROUTES "preserve-routes"
+#endif
+
+/*****************************************************************************/
+
+#endif /* __NM_VPN_PLUGIN_MACROS_H__ */
+


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