Blame gio/gproxyaddress.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2010 Collabora, Ltd.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Authors: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <config.h>
Packit ae235b
#include <glib.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include <gio/gsocketaddress.h>
Packit ae235b
Packit ae235b
#include "gproxyaddress.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gproxyaddress
Packit ae235b
 * @short_description: An internet address with proxy information
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * Support for proxied #GInetSocketAddress.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GProxyAddress:
Packit ae235b
 *
Packit ae235b
 * A #GInetSocketAddress representing a connection via a proxy server
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GProxyAddressClass:
Packit ae235b
 *
Packit ae235b
 * Class structure for #GProxyAddress.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  PROP_0,
Packit ae235b
  PROP_PROTOCOL,
Packit ae235b
  PROP_DESTINATION_PROTOCOL,
Packit ae235b
  PROP_DESTINATION_HOSTNAME,
Packit ae235b
  PROP_DESTINATION_PORT,
Packit ae235b
  PROP_USERNAME,
Packit ae235b
  PROP_PASSWORD,
Packit ae235b
  PROP_URI
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GProxyAddressPrivate
Packit ae235b
{
Packit ae235b
  gchar 	 *uri;
Packit ae235b
  gchar 	 *protocol;
Packit ae235b
  gchar		 *username;
Packit ae235b
  gchar		 *password;
Packit ae235b
  gchar 	 *dest_protocol;
Packit ae235b
  gchar 	 *dest_hostname;
Packit ae235b
  guint16 	  dest_port;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_PRIVATE (GProxyAddress, g_proxy_address, G_TYPE_INET_SOCKET_ADDRESS)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_proxy_address_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GProxyAddress *proxy = G_PROXY_ADDRESS (object);
Packit ae235b
Packit ae235b
  g_free (proxy->priv->uri);
Packit ae235b
  g_free (proxy->priv->protocol);
Packit ae235b
  g_free (proxy->priv->username);
Packit ae235b
  g_free (proxy->priv->password);
Packit ae235b
  g_free (proxy->priv->dest_hostname);
Packit ae235b
  g_free (proxy->priv->dest_protocol);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_proxy_address_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_proxy_address_set_property (GObject      *object,
Packit ae235b
			      guint         prop_id,
Packit ae235b
			      const GValue *value,
Packit ae235b
			      GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  GProxyAddress *proxy = G_PROXY_ADDRESS (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_PROTOCOL:
Packit ae235b
      g_free (proxy->priv->protocol);
Packit ae235b
      proxy->priv->protocol = g_value_dup_string (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_DESTINATION_PROTOCOL:
Packit ae235b
      g_free (proxy->priv->dest_protocol);
Packit ae235b
      proxy->priv->dest_protocol = g_value_dup_string (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_DESTINATION_HOSTNAME:
Packit ae235b
      g_free (proxy->priv->dest_hostname);
Packit ae235b
      proxy->priv->dest_hostname = g_value_dup_string (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_DESTINATION_PORT:
Packit ae235b
      proxy->priv->dest_port = g_value_get_uint (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_USERNAME:
Packit ae235b
      g_free (proxy->priv->username);
Packit ae235b
      proxy->priv->username = g_value_dup_string (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_PASSWORD:
Packit ae235b
      g_free (proxy->priv->password);
Packit ae235b
      proxy->priv->password = g_value_dup_string (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_URI:
Packit ae235b
      g_free (proxy->priv->uri);
Packit ae235b
      proxy->priv->uri = g_value_dup_string (value);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_proxy_address_get_property (GObject    *object,
Packit ae235b
			      guint       prop_id,
Packit ae235b
			      GValue     *value,
Packit ae235b
			      GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GProxyAddress *proxy = G_PROXY_ADDRESS (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
      case PROP_PROTOCOL:
Packit ae235b
	g_value_set_string (value, proxy->priv->protocol);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_DESTINATION_PROTOCOL:
Packit ae235b
	g_value_set_string (value, proxy->priv->dest_protocol);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_DESTINATION_HOSTNAME:
Packit ae235b
	g_value_set_string (value, proxy->priv->dest_hostname);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_DESTINATION_PORT:
Packit ae235b
	g_value_set_uint (value, proxy->priv->dest_port);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_USERNAME:
Packit ae235b
	g_value_set_string (value, proxy->priv->username);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_PASSWORD:
Packit ae235b
	g_value_set_string (value, proxy->priv->password);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_URI:
Packit ae235b
	g_value_set_string (value, proxy->priv->uri);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      default:
Packit ae235b
	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_proxy_address_class_init (GProxyAddressClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_proxy_address_finalize;
Packit ae235b
  gobject_class->set_property = g_proxy_address_set_property;
Packit ae235b
  gobject_class->get_property = g_proxy_address_get_property;
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
				   PROP_PROTOCOL,
Packit ae235b
				   g_param_spec_string ("protocol",
Packit ae235b
						       P_("Protocol"),
Packit ae235b
						       P_("The proxy protocol"),
Packit ae235b
						       NULL,
Packit ae235b
						       G_PARAM_READWRITE |
Packit ae235b
						       G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
						       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
				   PROP_USERNAME,
Packit ae235b
				   g_param_spec_string ("username",
Packit ae235b
						       P_("Username"),
Packit ae235b
						       P_("The proxy username"),
Packit ae235b
						       NULL,
Packit ae235b
						       G_PARAM_READWRITE |
Packit ae235b
						       G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
						       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
				   PROP_PASSWORD,
Packit ae235b
				   g_param_spec_string ("password",
Packit ae235b
						       P_("Password"),
Packit ae235b
						       P_("The proxy password"),
Packit ae235b
						       NULL,
Packit ae235b
						       G_PARAM_READWRITE |
Packit ae235b
						       G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
						       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GProxyAddress:destination-protocol:
Packit ae235b
   *
Packit ae235b
   * The protocol being spoke to the destination host, or %NULL if
Packit ae235b
   * the #GProxyAddress doesn't know.
Packit ae235b
   *
Packit ae235b
   * Since: 2.34
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
				   PROP_DESTINATION_PROTOCOL,
Packit ae235b
				   g_param_spec_string ("destination-protocol",
Packit ae235b
						       P_("Destionation Protocol"),
Packit ae235b
						       P_("The proxy destination protocol"),
Packit ae235b
						       NULL,
Packit ae235b
						       G_PARAM_READWRITE |
Packit ae235b
						       G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
						       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
				   PROP_DESTINATION_HOSTNAME,
Packit ae235b
				   g_param_spec_string ("destination-hostname",
Packit ae235b
						       P_("Destination Hostname"),
Packit ae235b
						       P_("The proxy destination hostname"),
Packit ae235b
						       NULL,
Packit ae235b
						       G_PARAM_READWRITE |
Packit ae235b
						       G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
						       G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
				   PROP_DESTINATION_PORT,
Packit ae235b
				   g_param_spec_uint ("destination-port",
Packit ae235b
						      P_("Destination Port"),
Packit ae235b
						      P_("The proxy destination port"),
Packit ae235b
						      0, 65535, 0,
Packit ae235b
						      G_PARAM_READWRITE |
Packit ae235b
						      G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
						      G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GProxyAddress:uri:
Packit ae235b
   *
Packit ae235b
   * The URI string that the proxy was constructed from (or %NULL
Packit ae235b
   * if the creator didn't specify this).
Packit ae235b
   *
Packit ae235b
   * Since: 2.34
Packit ae235b
   */
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
				   PROP_URI,
Packit ae235b
				   g_param_spec_string ("uri",
Packit ae235b
							P_("URI"),
Packit ae235b
							P_("The proxy’s URI"),
Packit ae235b
							NULL,
Packit ae235b
							G_PARAM_READWRITE |
Packit ae235b
							G_PARAM_CONSTRUCT_ONLY |
Packit ae235b
							G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_proxy_address_init (GProxyAddress *proxy)
Packit ae235b
{
Packit ae235b
  proxy->priv = g_proxy_address_get_instance_private (proxy);
Packit ae235b
  proxy->priv->protocol = NULL;
Packit ae235b
  proxy->priv->username = NULL;
Packit ae235b
  proxy->priv->password = NULL;
Packit ae235b
  proxy->priv->dest_hostname = NULL;
Packit ae235b
  proxy->priv->dest_port = 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_address_new:
Packit ae235b
 * @inetaddr: The proxy server #GInetAddress.
Packit ae235b
 * @port: The proxy server port.
Packit ae235b
 * @protocol: The proxy protocol to support, in lower case (e.g. socks, http).
Packit ae235b
 * @dest_hostname: The destination hostname the proxy should tunnel to.
Packit ae235b
 * @dest_port: The destination port to tunnel to.
Packit ae235b
 * @username: (nullable): The username to authenticate to the proxy server
Packit ae235b
 *     (or %NULL).
Packit ae235b
 * @password: (nullable): The password to authenticate to the proxy server
Packit ae235b
 *     (or %NULL).
Packit ae235b
 *
Packit ae235b
 * Creates a new #GProxyAddress for @inetaddr with @protocol that should
Packit ae235b
 * tunnel through @dest_hostname and @dest_port.
Packit ae235b
 *
Packit ae235b
 * (Note that this method doesn't set the #GProxyAddress:uri or
Packit ae235b
 * #GProxyAddress:destination-protocol fields; use g_object_new()
Packit ae235b
 * directly if you want to set those.)
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GProxyAddress
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GSocketAddress *
Packit ae235b
g_proxy_address_new (GInetAddress  *inetaddr,
Packit ae235b
		     guint16        port,
Packit ae235b
		     const gchar   *protocol,
Packit ae235b
		     const gchar   *dest_hostname,
Packit ae235b
		     guint16        dest_port,
Packit ae235b
		     const gchar   *username,
Packit ae235b
		     const gchar   *password)
Packit ae235b
{
Packit ae235b
  return g_object_new (G_TYPE_PROXY_ADDRESS,
Packit ae235b
		       "address", inetaddr,
Packit ae235b
		       "port", port,
Packit ae235b
		       "protocol", protocol,
Packit ae235b
		       "destination-hostname", dest_hostname,
Packit ae235b
		       "destination-port", dest_port,
Packit ae235b
		       "username", username,
Packit ae235b
		       "password", password,
Packit ae235b
		       NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_address_get_protocol:
Packit ae235b
 * @proxy: a #GProxyAddress
Packit ae235b
 *
Packit ae235b
 * Gets @proxy's protocol. eg, "socks" or "http"
Packit ae235b
 *
Packit ae235b
 * Returns: the @proxy's protocol
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_proxy_address_get_protocol (GProxyAddress *proxy)
Packit ae235b
{
Packit ae235b
  return proxy->priv->protocol;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_address_get_destination_protocol:
Packit ae235b
 * @proxy: a #GProxyAddress
Packit ae235b
 *
Packit ae235b
 * Gets the protocol that is being spoken to the destination
Packit ae235b
 * server; eg, "http" or "ftp".
Packit ae235b
 *
Packit ae235b
 * Returns: the @proxy's destination protocol
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_proxy_address_get_destination_protocol (GProxyAddress *proxy)
Packit ae235b
{
Packit ae235b
  return proxy->priv->dest_protocol;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_address_get_destination_hostname:
Packit ae235b
 * @proxy: a #GProxyAddress
Packit ae235b
 *
Packit ae235b
 * Gets @proxy's destination hostname; that is, the name of the host
Packit ae235b
 * that will be connected to via the proxy, not the name of the proxy
Packit ae235b
 * itself.
Packit ae235b
 *
Packit ae235b
 * Returns: the @proxy's destination hostname
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_proxy_address_get_destination_hostname (GProxyAddress *proxy)
Packit ae235b
{
Packit ae235b
  return proxy->priv->dest_hostname;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_address_get_destination_port:
Packit ae235b
 * @proxy: a #GProxyAddress
Packit ae235b
 *
Packit ae235b
 * Gets @proxy's destination port; that is, the port on the
Packit ae235b
 * destination host that will be connected to via the proxy, not the
Packit ae235b
 * port number of the proxy itself.
Packit ae235b
 *
Packit ae235b
 * Returns: the @proxy's destination port
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
guint16
Packit ae235b
g_proxy_address_get_destination_port (GProxyAddress *proxy)
Packit ae235b
{
Packit ae235b
  return proxy->priv->dest_port;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_address_get_username:
Packit ae235b
 * @proxy: a #GProxyAddress
Packit ae235b
 *
Packit ae235b
 * Gets @proxy's username.
Packit ae235b
 *
Packit ae235b
 * Returns: the @proxy's username
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_proxy_address_get_username (GProxyAddress *proxy)
Packit ae235b
{
Packit ae235b
  return proxy->priv->username;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_address_get_password:
Packit ae235b
 * @proxy: a #GProxyAddress
Packit ae235b
 *
Packit ae235b
 * Gets @proxy's password.
Packit ae235b
 *
Packit ae235b
 * Returns: the @proxy's password
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_proxy_address_get_password (GProxyAddress *proxy)
Packit ae235b
{
Packit ae235b
  return proxy->priv->password;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_address_get_uri:
Packit ae235b
 * @proxy: a #GProxyAddress
Packit ae235b
 *
Packit ae235b
 * Gets the proxy URI that @proxy was constructed from.
Packit ae235b
 *
Packit ae235b
 * Returns: the @proxy's URI, or %NULL if unknown
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_proxy_address_get_uri (GProxyAddress *proxy)
Packit ae235b
{
Packit ae235b
  return proxy->priv->uri;
Packit ae235b
}