Blame gio/gproxy.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
 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gproxy.h"
Packit ae235b
Packit ae235b
#include "giomodule.h"
Packit ae235b
#include "giomodule-priv.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gproxy
Packit ae235b
 * @short_description: Interface for proxy handling
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * A #GProxy handles connecting to a remote host via a given type of
Packit ae235b
 * proxy server. It is implemented by the 'gio-proxy' extension point.
Packit ae235b
 * The extensions are named after their proxy protocol name. As an
Packit ae235b
 * example, a SOCKS5 proxy implementation can be retrieved with the
Packit ae235b
 * name 'socks5' using the function
Packit ae235b
 * g_io_extension_point_get_extension_by_name().
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
Packit ae235b
G_DEFINE_INTERFACE (GProxy, g_proxy, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_proxy_default_init (GProxyInterface *iface)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_get_default_for_protocol:
Packit ae235b
 * @protocol: the proxy protocol name (e.g. http, socks, etc)
Packit ae235b
 *
Packit ae235b
 * Lookup "gio-proxy" extension point for a proxy implementation that supports
Packit ae235b
 * specified protocol.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): return a #GProxy or NULL if protocol
Packit ae235b
 *               is not supported.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
GProxy *
Packit ae235b
g_proxy_get_default_for_protocol (const gchar *protocol)
Packit ae235b
{
Packit ae235b
  GIOExtensionPoint *ep;
Packit ae235b
  GIOExtension *extension;
Packit ae235b
Packit ae235b
  /* Ensure proxy modules loaded */
Packit ae235b
  _g_io_modules_ensure_loaded ();
Packit ae235b
Packit ae235b
  ep = g_io_extension_point_lookup (G_PROXY_EXTENSION_POINT_NAME);
Packit ae235b
Packit ae235b
  extension = g_io_extension_point_get_extension_by_name (ep, protocol);
Packit ae235b
Packit ae235b
  if (extension)
Packit ae235b
      return g_object_new (g_io_extension_get_type (extension), NULL);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_connect:
Packit ae235b
 * @proxy: a #GProxy
Packit ae235b
 * @connection: a #GIOStream
Packit ae235b
 * @proxy_address: a #GProxyAddress
Packit ae235b
 * @cancellable: (nullable): a #GCancellable
Packit ae235b
 * @error: return #GError
Packit ae235b
 *
Packit ae235b
 * Given @connection to communicate with a proxy (eg, a
Packit ae235b
 * #GSocketConnection that is connected to the proxy server), this
Packit ae235b
 * does the necessary handshake to connect to @proxy_address, and if
Packit ae235b
 * required, wraps the #GIOStream to handle proxy payload.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GIOStream that will replace @connection. This might
Packit ae235b
 *               be the same as @connection, in which case a reference
Packit ae235b
 *               will be added.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GIOStream *
Packit ae235b
g_proxy_connect (GProxy            *proxy,
Packit ae235b
		 GIOStream         *connection,
Packit ae235b
		 GProxyAddress     *proxy_address,
Packit ae235b
		 GCancellable      *cancellable,
Packit ae235b
		 GError           **error)
Packit ae235b
{
Packit ae235b
  GProxyInterface *iface;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_PROXY (proxy), NULL);
Packit ae235b
Packit ae235b
  iface = G_PROXY_GET_IFACE (proxy);
Packit ae235b
Packit ae235b
  return (* iface->connect) (proxy,
Packit ae235b
			     connection,
Packit ae235b
			     proxy_address,
Packit ae235b
			     cancellable,
Packit ae235b
			     error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_connect_async:
Packit ae235b
 * @proxy: a #GProxy
Packit ae235b
 * @connection: a #GIOStream
Packit ae235b
 * @proxy_address: a #GProxyAddress
Packit ae235b
 * @cancellable: (nullable): a #GCancellable
Packit ae235b
 * @callback: (scope async): a #GAsyncReadyCallback
Packit ae235b
 * @user_data: (closure): callback data
Packit ae235b
 *
Packit ae235b
 * Asynchronous version of g_proxy_connect().
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_proxy_connect_async (GProxy               *proxy,
Packit ae235b
		       GIOStream            *connection,
Packit ae235b
		       GProxyAddress        *proxy_address,
Packit ae235b
		       GCancellable         *cancellable,
Packit ae235b
		       GAsyncReadyCallback   callback,
Packit ae235b
		       gpointer              user_data)
Packit ae235b
{
Packit ae235b
  GProxyInterface *iface;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_PROXY (proxy));
Packit ae235b
Packit ae235b
  iface = G_PROXY_GET_IFACE (proxy);
Packit ae235b
Packit ae235b
  (* iface->connect_async) (proxy,
Packit ae235b
			    connection,
Packit ae235b
			    proxy_address,
Packit ae235b
			    cancellable,
Packit ae235b
			    callback,
Packit ae235b
			    user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_connect_finish:
Packit ae235b
 * @proxy: a #GProxy
Packit ae235b
 * @result: a #GAsyncResult
Packit ae235b
 * @error: return #GError
Packit ae235b
 *
Packit ae235b
 * See g_proxy_connect().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GIOStream.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GIOStream *
Packit ae235b
g_proxy_connect_finish (GProxy       *proxy,
Packit ae235b
			GAsyncResult *result,
Packit ae235b
			GError      **error)
Packit ae235b
{
Packit ae235b
  GProxyInterface *iface;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_PROXY (proxy), NULL);
Packit ae235b
Packit ae235b
  iface = G_PROXY_GET_IFACE (proxy);
Packit ae235b
Packit ae235b
  return (* iface->connect_finish) (proxy, result, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_proxy_supports_hostname:
Packit ae235b
 * @proxy: a #GProxy
Packit ae235b
 *
Packit ae235b
 * Some proxy protocols expect to be passed a hostname, which they
Packit ae235b
 * will resolve to an IP address themselves. Others, like SOCKS4, do
Packit ae235b
 * not allow this. This function will return %FALSE if @proxy is
Packit ae235b
 * implementing such a protocol. When %FALSE is returned, the caller
Packit ae235b
 * should resolve the destination hostname first, and then pass a
Packit ae235b
 * #GProxyAddress containing the stringified IP address to
Packit ae235b
 * g_proxy_connect() or g_proxy_connect_async().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if hostname resolution is supported.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_proxy_supports_hostname (GProxy *proxy)
Packit ae235b
{
Packit ae235b
  GProxyInterface *iface;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_PROXY (proxy), FALSE);
Packit ae235b
Packit ae235b
  iface = G_PROXY_GET_IFACE (proxy);
Packit ae235b
Packit ae235b
  return (* iface->supports_hostname) (proxy);
Packit ae235b
}