1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Collabora, Ltd.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Authors: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
27 #include <gio/gsocketaddress.h>
29 #include "gproxyaddress.h"
35 * A [class@Gio.InetSocketAddress] representing a connection via a proxy server.
43 * Class structure for #GProxyAddress.
52 PROP_DESTINATION_PROTOCOL,
53 PROP_DESTINATION_HOSTNAME,
54 PROP_DESTINATION_PORT,
60 struct _GProxyAddressPrivate
71 G_DEFINE_TYPE_WITH_PRIVATE (GProxyAddress, g_proxy_address, G_TYPE_INET_SOCKET_ADDRESS)
74 g_proxy_address_finalize (GObject *object)
76 GProxyAddress *proxy = G_PROXY_ADDRESS (object);
78 g_free (proxy->priv->uri);
79 g_free (proxy->priv->protocol);
80 g_free (proxy->priv->username);
81 g_free (proxy->priv->password);
82 g_free (proxy->priv->dest_hostname);
83 g_free (proxy->priv->dest_protocol);
85 G_OBJECT_CLASS (g_proxy_address_parent_class)->finalize (object);
89 g_proxy_address_set_property (GObject *object,
94 GProxyAddress *proxy = G_PROXY_ADDRESS (object);
99 g_free (proxy->priv->protocol);
100 proxy->priv->protocol = g_value_dup_string (value);
103 case PROP_DESTINATION_PROTOCOL:
104 g_free (proxy->priv->dest_protocol);
105 proxy->priv->dest_protocol = g_value_dup_string (value);
108 case PROP_DESTINATION_HOSTNAME:
109 g_free (proxy->priv->dest_hostname);
110 proxy->priv->dest_hostname = g_value_dup_string (value);
113 case PROP_DESTINATION_PORT:
114 proxy->priv->dest_port = g_value_get_uint (value);
118 g_free (proxy->priv->username);
119 proxy->priv->username = g_value_dup_string (value);
123 g_free (proxy->priv->password);
124 proxy->priv->password = g_value_dup_string (value);
128 g_free (proxy->priv->uri);
129 proxy->priv->uri = g_value_dup_string (value);
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138 g_proxy_address_get_property (GObject *object,
143 GProxyAddress *proxy = G_PROXY_ADDRESS (object);
148 g_value_set_string (value, proxy->priv->protocol);
151 case PROP_DESTINATION_PROTOCOL:
152 g_value_set_string (value, proxy->priv->dest_protocol);
155 case PROP_DESTINATION_HOSTNAME:
156 g_value_set_string (value, proxy->priv->dest_hostname);
159 case PROP_DESTINATION_PORT:
160 g_value_set_uint (value, proxy->priv->dest_port);
164 g_value_set_string (value, proxy->priv->username);
168 g_value_set_string (value, proxy->priv->password);
172 g_value_set_string (value, proxy->priv->uri);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 g_proxy_address_class_init (GProxyAddressClass *klass)
183 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
185 gobject_class->finalize = g_proxy_address_finalize;
186 gobject_class->set_property = g_proxy_address_set_property;
187 gobject_class->get_property = g_proxy_address_get_property;
190 * GProxyAddress:protocol:
192 * The proxy protocol.
196 g_object_class_install_property (gobject_class,
198 g_param_spec_string ("protocol", NULL, NULL,
201 G_PARAM_CONSTRUCT_ONLY |
202 G_PARAM_STATIC_STRINGS));
205 * GProxyAddress:username:
207 * The proxy username.
211 g_object_class_install_property (gobject_class,
213 g_param_spec_string ("username", NULL, NULL,
216 G_PARAM_CONSTRUCT_ONLY |
217 G_PARAM_STATIC_STRINGS));
220 * GProxyAddress:password:
222 * The proxy password.
226 g_object_class_install_property (gobject_class,
228 g_param_spec_string ("password", NULL, NULL,
231 G_PARAM_CONSTRUCT_ONLY |
232 G_PARAM_STATIC_STRINGS));
235 * GProxyAddress:destination-protocol:
237 * The protocol being spoke to the destination host, or %NULL if
238 * the #GProxyAddress doesn't know.
242 g_object_class_install_property (gobject_class,
243 PROP_DESTINATION_PROTOCOL,
244 g_param_spec_string ("destination-protocol", NULL, NULL,
247 G_PARAM_CONSTRUCT_ONLY |
248 G_PARAM_STATIC_STRINGS));
251 * GProxyAddress:destination-hostname:
253 * The proxy destination hostname.
257 g_object_class_install_property (gobject_class,
258 PROP_DESTINATION_HOSTNAME,
259 g_param_spec_string ("destination-hostname", NULL, NULL,
262 G_PARAM_CONSTRUCT_ONLY |
263 G_PARAM_STATIC_STRINGS));
266 * GProxyAddress:destination-port:
268 * The proxy destination port.
272 g_object_class_install_property (gobject_class,
273 PROP_DESTINATION_PORT,
274 g_param_spec_uint ("destination-port", NULL, NULL,
277 G_PARAM_CONSTRUCT_ONLY |
278 G_PARAM_STATIC_STRINGS));
283 * The URI string that the proxy was constructed from (or %NULL
284 * if the creator didn't specify this).
288 g_object_class_install_property (gobject_class,
290 g_param_spec_string ("uri", NULL, NULL,
293 G_PARAM_CONSTRUCT_ONLY |
294 G_PARAM_STATIC_STRINGS));
298 g_proxy_address_init (GProxyAddress *proxy)
300 proxy->priv = g_proxy_address_get_instance_private (proxy);
301 proxy->priv->protocol = NULL;
302 proxy->priv->username = NULL;
303 proxy->priv->password = NULL;
304 proxy->priv->dest_hostname = NULL;
305 proxy->priv->dest_port = 0;
309 * g_proxy_address_new:
310 * @inetaddr: The proxy server #GInetAddress.
311 * @port: The proxy server port.
312 * @protocol: The proxy protocol to support, in lower case (e.g. socks, http).
313 * @dest_hostname: The destination hostname the proxy should tunnel to.
314 * @dest_port: The destination port to tunnel to.
315 * @username: (nullable): The username to authenticate to the proxy server
317 * @password: (nullable): The password to authenticate to the proxy server
320 * Creates a new #GProxyAddress for @inetaddr with @protocol that should
321 * tunnel through @dest_hostname and @dest_port.
323 * (Note that this method doesn't set the #GProxyAddress:uri or
324 * #GProxyAddress:destination-protocol fields; use g_object_new()
325 * directly if you want to set those.)
327 * Returns: a new #GProxyAddress
332 g_proxy_address_new (GInetAddress *inetaddr,
334 const gchar *protocol,
335 const gchar *dest_hostname,
337 const gchar *username,
338 const gchar *password)
340 return g_object_new (G_TYPE_PROXY_ADDRESS,
343 "protocol", protocol,
344 "destination-hostname", dest_hostname,
345 "destination-port", dest_port,
346 "username", username,
347 "password", password,
353 * g_proxy_address_get_protocol:
354 * @proxy: a #GProxyAddress
356 * Gets @proxy's protocol. eg, "socks" or "http"
358 * Returns: the @proxy's protocol
363 g_proxy_address_get_protocol (GProxyAddress *proxy)
365 return proxy->priv->protocol;
369 * g_proxy_address_get_destination_protocol:
370 * @proxy: a #GProxyAddress
372 * Gets the protocol that is being spoken to the destination
373 * server; eg, "http" or "ftp".
375 * Returns: the @proxy's destination protocol
380 g_proxy_address_get_destination_protocol (GProxyAddress *proxy)
382 return proxy->priv->dest_protocol;
386 * g_proxy_address_get_destination_hostname:
387 * @proxy: a #GProxyAddress
389 * Gets @proxy's destination hostname; that is, the name of the host
390 * that will be connected to via the proxy, not the name of the proxy
393 * Returns: the @proxy's destination hostname
398 g_proxy_address_get_destination_hostname (GProxyAddress *proxy)
400 return proxy->priv->dest_hostname;
404 * g_proxy_address_get_destination_port:
405 * @proxy: a #GProxyAddress
407 * Gets @proxy's destination port; that is, the port on the
408 * destination host that will be connected to via the proxy, not the
409 * port number of the proxy itself.
411 * Returns: the @proxy's destination port
416 g_proxy_address_get_destination_port (GProxyAddress *proxy)
418 return proxy->priv->dest_port;
422 * g_proxy_address_get_username:
423 * @proxy: a #GProxyAddress
425 * Gets @proxy's username.
427 * Returns: (nullable): the @proxy's username
432 g_proxy_address_get_username (GProxyAddress *proxy)
434 return proxy->priv->username;
438 * g_proxy_address_get_password:
439 * @proxy: a #GProxyAddress
441 * Gets @proxy's password.
443 * Returns: (nullable): the @proxy's password
448 g_proxy_address_get_password (GProxyAddress *proxy)
450 return proxy->priv->password;
455 * g_proxy_address_get_uri:
456 * @proxy: a #GProxyAddress
458 * Gets the proxy URI that @proxy was constructed from.
460 * Returns: (nullable): the @proxy's URI, or %NULL if unknown
465 g_proxy_address_get_uri (GProxyAddress *proxy)
467 return proxy->priv->uri;