diff --git a/src/Makefile.am b/src/Makefile.am index 6e2742a8d..db49154a0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -442,6 +442,8 @@ libNetworkManager_la_SOURCES = \ nm-exported-object.h \ nm-firewall-manager.c \ nm-firewall-manager.h \ + nm-proxy-config.c \ + nm-proxy-config.h \ nm-ip4-config.c \ nm-ip4-config.h \ nm-ip6-config.c \ @@ -566,6 +568,8 @@ nm_iface_helper_SOURCES = \ \ nm-exported-object.c \ nm-exported-object.h \ + nm-proxy-config.c \ + nm-proxy-config.h \ nm-ip4-config.c \ nm-ip4-config.h \ nm-ip6-config.c \ diff --git a/src/nm-proxy-config.c b/src/nm-proxy-config.c new file mode 100644 index 000000000..51bdadad2 --- /dev/null +++ b/src/nm-proxy-config.c @@ -0,0 +1,245 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * (C) Copyright 2016 Atul Anand . + */ + +#include "nm-default.h" + +#include "nm-proxy-config.h" + +#include + +#include "nm-core-internal.h" + +#define NM_PROXY_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_PROXY_CONFIG, NMProxyConfigPrivate)) + +G_DEFINE_TYPE (NMProxyConfig, nm_proxy_config, G_TYPE_OBJECT) + +typedef struct { + NMProxyConfigMethod method; + GPtrArray *proxies; + GPtrArray *excludes; + gboolean browser_only; + char *pac_url; + char *pac_script; +} NMProxyConfigPrivate; + +NMProxyConfig * +nm_proxy_config_new (void) +{ + return NM_PROXY_CONFIG (g_object_new (NM_TYPE_PROXY_CONFIG, NULL)); +} + +void +nm_proxy_config_set_method (NMProxyConfig *config, NMProxyConfigMethod method) +{ + NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config); + + priv->method = method; +} + +NMProxyConfigMethod +nm_proxy_config_get_method (const NMProxyConfig *config) +{ + NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config); + + return priv->method; +} + +void +nm_proxy_config_merge_setting (NMProxyConfig *config, NMSettingProxy *setting) +{ + const char *tmp = NULL; + guint32 port = 0; + NMProxyConfigPrivate *priv; + NMSettingProxyMethod method; + + if (!setting) + return; + + g_return_if_fail (NM_IS_SETTING_PROXY (setting)); + + priv = NM_PROXY_CONFIG_GET_PRIVATE (config); + + g_ptr_array_free (priv->proxies, TRUE); + g_ptr_array_free (priv->excludes, TRUE); + g_free (priv->pac_script); + + priv->proxies = NULL; + priv->excludes = NULL; + priv->pac_script = NULL; + + method = nm_setting_proxy_get_method (setting); + switch (method) { + case NM_SETTING_PROXY_METHOD_AUTO: + priv->method = NM_PROXY_CONFIG_METHOD_AUTO; + + /* Free DHCP Obtained PAC Url (i.e Option 252) + * only when libnm overrides it. + */ + tmp = nm_setting_proxy_get_pac_url (setting); + if (tmp) { + g_free (priv->pac_url); + priv->pac_url = g_strdup (tmp); + } + + tmp = nm_setting_proxy_get_pac_script (setting); + priv->pac_script = g_strdup (tmp); + + break; + case NM_SETTING_PROXY_METHOD_MANUAL: + priv->method = NM_PROXY_CONFIG_METHOD_MANUAL; + + priv->excludes = _nm_utils_strv_to_ptrarray ((char **) nm_setting_proxy_get_no_proxy_for (setting)); + + priv->proxies = g_ptr_array_new_with_free_func (g_free); + + tmp = nm_setting_proxy_get_http_proxy (setting); + port = nm_setting_proxy_get_http_port (setting); + + /* If HTTP Proxy has been selected for all Protocols + * set up a generic proxy in PacRunner i.e without a + * protocol prefix. + */ + if (nm_setting_proxy_get_http_default (setting)) { + if (tmp && port) + g_ptr_array_add (priv->proxies, g_strdup_printf ("%s:%u/", tmp, port)); + break; + } + + if (tmp && port) + g_ptr_array_add (priv->proxies, g_strdup_printf ("http://%s:%u/", tmp, port)); + + tmp = nm_setting_proxy_get_ssl_proxy (setting); + port = nm_setting_proxy_get_ssl_port (setting); + if (tmp && port) + g_ptr_array_add (priv->proxies, g_strdup_printf ("https://%s:%u/", tmp, port)); + + tmp = nm_setting_proxy_get_ftp_proxy (setting); + port = nm_setting_proxy_get_ftp_port (setting); + if (tmp && port) + g_ptr_array_add (priv->proxies, g_strdup_printf ("ftp://%s:%u/", tmp, port)); + + tmp = nm_setting_proxy_get_socks_proxy (setting); + port = nm_setting_proxy_get_socks_port (setting); + if (tmp && port) + g_ptr_array_add (priv->proxies, g_strdup_printf (nm_setting_proxy_get_socks_version_5 (setting) ? + "socks5://%s:%u/" : "socks4://%s:%u/", tmp, port)); + + break; + case NM_SETTING_PROXY_METHOD_NONE: + priv->method = NM_PROXY_CONFIG_METHOD_NONE; + /* Do Nothing */ + } + + priv->browser_only = nm_setting_proxy_get_browser_only (setting); +} + +char ** +nm_proxy_config_get_proxies (const NMProxyConfig *config) +{ + NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config); + + return _nm_utils_ptrarray_to_strv (priv->proxies); +} + +char ** +nm_proxy_config_get_excludes (const NMProxyConfig *config) +{ + NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config); + + return _nm_utils_ptrarray_to_strv (priv->excludes); +} + +gboolean +nm_proxy_config_get_browser_only (const NMProxyConfig *config) +{ + NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config); + + return priv->browser_only; +} + +void +nm_proxy_config_set_pac_url (NMProxyConfig *config, const char *url) +{ + NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config); + + g_free (priv->pac_url); + priv->pac_url = g_strdup (url); +} + +const char * +nm_proxy_config_get_pac_url (const NMProxyConfig *config) +{ + NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config); + + return priv->pac_url; +} + +void +nm_proxy_config_set_pac_script (NMProxyConfig *config, const char *script) +{ + NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config); + + g_free (priv->pac_script); + priv->pac_script = g_strdup (script); +} + +const char * +nm_proxy_config_get_pac_script (const NMProxyConfig *config) +{ + NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config); + + return priv->pac_script; +} + +static void +nm_proxy_config_init (NMProxyConfig *config) +{ + NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (config); + + priv->method = NM_PROXY_CONFIG_METHOD_NONE; + priv->proxies = g_ptr_array_new_with_free_func (g_free); + priv->excludes = g_ptr_array_new_with_free_func (g_free); +} + +static void +finalize (GObject *object) +{ + NMProxyConfig *self = NM_PROXY_CONFIG (object); + NMProxyConfigPrivate *priv = NM_PROXY_CONFIG_GET_PRIVATE (self); + + if (priv->proxies) + g_ptr_array_free (priv->proxies, TRUE); + if (priv->excludes) + g_ptr_array_free (priv->excludes, TRUE); + g_free (priv->pac_url); + g_free (priv->pac_script); + + G_OBJECT_CLASS (nm_proxy_config_parent_class)->finalize (object); +} + +static void +nm_proxy_config_class_init (NMProxyConfigClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (NMProxyConfigPrivate)); + + object_class->finalize = finalize; +} diff --git a/src/nm-proxy-config.h b/src/nm-proxy-config.h new file mode 100644 index 000000000..3bbf8222c --- /dev/null +++ b/src/nm-proxy-config.h @@ -0,0 +1,68 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * (C) Copyright 2016 Atul Anand . + */ + +#ifndef __NETWORKMANAGER_PROXY_CONFIG_H__ +#define __NETWORKMANAGER_PROXY_CONFIG_H__ + +#include "nm-setting-proxy.h" + +typedef enum { + NM_PROXY_CONFIG_METHOD_AUTO = 0, + NM_PROXY_CONFIG_METHOD_MANUAL, + NM_PROXY_CONFIG_METHOD_NONE +} NMProxyConfigMethod; + +#define NM_TYPE_PROXY_CONFIG (nm_proxy_config_get_type ()) +#define NM_PROXY_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_PROXY_CONFIG, NMProxyConfig)) +#define NM_PROXY_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_PROXY_CONFIG, NMProxyConfigClass)) +#define NM_IS_PROXY_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_PROXY_CONFIG)) +#define NM_IS_PROXY_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_PROXY_CONFIG)) +#define NM_PROXY_CONFIG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_PROXY_CONFIG, NMProxyConfigClass)) + +struct _NMProxyConfig { + GObject parent; +}; + +typedef struct { + GObjectClass parent; +} NMProxyConfigClass; + +GType nm_proxy_config_get_type (void); + +NMProxyConfig * nm_proxy_config_new (void); + +void nm_proxy_config_set_method (NMProxyConfig *config, NMProxyConfigMethod method); +NMProxyConfigMethod nm_proxy_config_get_method (const NMProxyConfig *config); + +void nm_proxy_config_merge_setting (NMProxyConfig *config, NMSettingProxy *setting); + +char ** nm_proxy_config_get_proxies (const NMProxyConfig *config); + +char ** nm_proxy_config_get_excludes (const NMProxyConfig *config); + +gboolean nm_proxy_config_get_browser_only (const NMProxyConfig *config); + +void nm_proxy_config_set_pac_url (NMProxyConfig *config, const char *url); +const char * nm_proxy_config_get_pac_url (const NMProxyConfig *config); + +void nm_proxy_config_set_pac_script (NMProxyConfig *config, const char *script); +const char * nm_proxy_config_get_pac_script (const NMProxyConfig *config); + +#endif /* __NETWORKMANAGER_PROXY_CONFIG_H__ */ diff --git a/src/nm-types.h b/src/nm-types.h index 8d8c02af6..5b21fb27d 100644 --- a/src/nm-types.h +++ b/src/nm-types.h @@ -42,6 +42,7 @@ typedef struct _NMDefaultRouteManager NMDefaultRouteManager; typedef struct _NMDevice NMDevice; typedef struct _NMDhcp4Config NMDhcp4Config; typedef struct _NMDhcp6Config NMDhcp6Config; +typedef struct _NMProxyConfig NMProxyConfig; typedef struct _NMIP4Config NMIP4Config; typedef struct _NMIP6Config NMIP6Config; typedef struct _NMManager NMManager;