Work around test failure in gdbus-names
[platform/upstream/glib.git] / gio / gproxyaddress.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2010 Collabora, Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Authors: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
19  */
20
21 #include <config.h>
22 #include <glib.h>
23 #include <string.h>
24
25 #include <gio/gsocketaddress.h>
26
27 #include "gproxyaddress.h"
28 #include "glibintl.h"
29
30 /**
31  * SECTION:gproxyaddress
32  * @short_description: An internet address with proxy information
33  * @include: gio/gio.h
34  *
35  * Support for proxied #GInetSocketAddress.
36  */
37
38 /**
39  * GProxyAddress:
40  *
41  * A #GInetSocketAddress representing a connection via a proxy server
42  *
43  * Since: 2.26
44  **/
45
46 enum
47 {
48   PROP_0,
49   PROP_PROTOCOL,
50   PROP_DESTINATION_PROTOCOL,
51   PROP_DESTINATION_HOSTNAME,
52   PROP_DESTINATION_PORT,
53   PROP_USERNAME,
54   PROP_PASSWORD,
55   PROP_URI
56 };
57
58 struct _GProxyAddressPrivate
59 {
60   gchar          *uri;
61   gchar          *protocol;
62   gchar          *username;
63   gchar          *password;
64   gchar          *dest_protocol;
65   gchar          *dest_hostname;
66   guint16         dest_port;
67 };
68
69 G_DEFINE_TYPE_WITH_PRIVATE (GProxyAddress, g_proxy_address, G_TYPE_INET_SOCKET_ADDRESS)
70
71 static void
72 g_proxy_address_finalize (GObject *object)
73 {
74   GProxyAddress *proxy = G_PROXY_ADDRESS (object);
75
76   g_free (proxy->priv->uri);
77   g_free (proxy->priv->protocol);
78   g_free (proxy->priv->username);
79   g_free (proxy->priv->password);
80   g_free (proxy->priv->dest_hostname);
81   g_free (proxy->priv->dest_protocol);
82
83   G_OBJECT_CLASS (g_proxy_address_parent_class)->finalize (object);
84 }
85
86 static void
87 g_proxy_address_set_property (GObject      *object,
88                               guint         prop_id,
89                               const GValue *value,
90                               GParamSpec   *pspec)
91 {
92   GProxyAddress *proxy = G_PROXY_ADDRESS (object);
93
94   switch (prop_id)
95     {
96     case PROP_PROTOCOL:
97       g_free (proxy->priv->protocol);
98       proxy->priv->protocol = g_value_dup_string (value);
99       break;
100
101     case PROP_DESTINATION_PROTOCOL:
102       g_free (proxy->priv->dest_protocol);
103       proxy->priv->dest_protocol = g_value_dup_string (value);
104       break;
105
106     case PROP_DESTINATION_HOSTNAME:
107       g_free (proxy->priv->dest_hostname);
108       proxy->priv->dest_hostname = g_value_dup_string (value);
109       break;
110
111     case PROP_DESTINATION_PORT:
112       proxy->priv->dest_port = g_value_get_uint (value);
113       break;
114
115     case PROP_USERNAME:
116       g_free (proxy->priv->username);
117       proxy->priv->username = g_value_dup_string (value);
118       break;
119
120     case PROP_PASSWORD:
121       g_free (proxy->priv->password);
122       proxy->priv->password = g_value_dup_string (value);
123       break;
124
125     case PROP_URI:
126       g_free (proxy->priv->uri);
127       proxy->priv->uri = g_value_dup_string (value);
128       break;
129
130     default:
131       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132     }
133 }
134
135 static void
136 g_proxy_address_get_property (GObject    *object,
137                               guint       prop_id,
138                               GValue     *value,
139                               GParamSpec *pspec)
140 {
141   GProxyAddress *proxy = G_PROXY_ADDRESS (object);
142
143   switch (prop_id)
144     {
145       case PROP_PROTOCOL:
146         g_value_set_string (value, proxy->priv->protocol);
147         break;
148
149       case PROP_DESTINATION_PROTOCOL:
150         g_value_set_string (value, proxy->priv->dest_protocol);
151         break;
152
153       case PROP_DESTINATION_HOSTNAME:
154         g_value_set_string (value, proxy->priv->dest_hostname);
155         break;
156
157       case PROP_DESTINATION_PORT:
158         g_value_set_uint (value, proxy->priv->dest_port);
159         break;
160
161       case PROP_USERNAME:
162         g_value_set_string (value, proxy->priv->username);
163         break;
164
165       case PROP_PASSWORD:
166         g_value_set_string (value, proxy->priv->password);
167         break;
168
169       case PROP_URI:
170         g_value_set_string (value, proxy->priv->uri);
171         break;
172
173       default:
174         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175     }
176 }
177
178 static void
179 g_proxy_address_class_init (GProxyAddressClass *klass)
180 {
181   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
182
183   gobject_class->finalize = g_proxy_address_finalize;
184   gobject_class->set_property = g_proxy_address_set_property;
185   gobject_class->get_property = g_proxy_address_get_property;
186
187   g_object_class_install_property (gobject_class,
188                                    PROP_PROTOCOL,
189                                    g_param_spec_string ("protocol",
190                                                        P_("Protocol"),
191                                                        P_("The proxy protocol"),
192                                                        NULL,
193                                                        G_PARAM_READWRITE |
194                                                        G_PARAM_CONSTRUCT_ONLY |
195                                                        G_PARAM_STATIC_STRINGS));
196
197   g_object_class_install_property (gobject_class,
198                                    PROP_USERNAME,
199                                    g_param_spec_string ("username",
200                                                        P_("Username"),
201                                                        P_("The proxy username"),
202                                                        NULL,
203                                                        G_PARAM_READWRITE |
204                                                        G_PARAM_CONSTRUCT_ONLY |
205                                                        G_PARAM_STATIC_STRINGS));
206
207   g_object_class_install_property (gobject_class,
208                                    PROP_PASSWORD,
209                                    g_param_spec_string ("password",
210                                                        P_("Password"),
211                                                        P_("The proxy password"),
212                                                        NULL,
213                                                        G_PARAM_READWRITE |
214                                                        G_PARAM_CONSTRUCT_ONLY |
215                                                        G_PARAM_STATIC_STRINGS));
216
217   /**
218    * GProxyAddress:destination-protocol:
219    *
220    * The protocol being spoke to the destination host, or %NULL if
221    * the #GProxyAddress doesn't know.
222    *
223    * Since: 2.34
224    */
225   g_object_class_install_property (gobject_class,
226                                    PROP_DESTINATION_PROTOCOL,
227                                    g_param_spec_string ("destination-protocol",
228                                                        P_("Destionation Protocol"),
229                                                        P_("The proxy destination protocol"),
230                                                        NULL,
231                                                        G_PARAM_READWRITE |
232                                                        G_PARAM_CONSTRUCT_ONLY |
233                                                        G_PARAM_STATIC_STRINGS));
234
235   g_object_class_install_property (gobject_class,
236                                    PROP_DESTINATION_HOSTNAME,
237                                    g_param_spec_string ("destination-hostname",
238                                                        P_("Destination Hostname"),
239                                                        P_("The proxy destination hostname"),
240                                                        NULL,
241                                                        G_PARAM_READWRITE |
242                                                        G_PARAM_CONSTRUCT_ONLY |
243                                                        G_PARAM_STATIC_STRINGS));
244
245   g_object_class_install_property (gobject_class,
246                                    PROP_DESTINATION_PORT,
247                                    g_param_spec_uint ("destination-port",
248                                                       P_("Destination Port"),
249                                                       P_("The proxy destination port"),
250                                                       0, 65535, 0,
251                                                       G_PARAM_READWRITE |
252                                                       G_PARAM_CONSTRUCT_ONLY |
253                                                       G_PARAM_STATIC_STRINGS));
254
255   /**
256    * GProxyAddress:uri:
257    *
258    * The URI string that the proxy was constructed from (or %NULL
259    * if the creator didn't specify this).
260    *
261    * Since: 2.34
262    */
263   g_object_class_install_property (gobject_class,
264                                    PROP_URI,
265                                    g_param_spec_string ("uri",
266                                                         P_("URI"),
267                                                         P_("The proxy's URI"),
268                                                         NULL,
269                                                         G_PARAM_READWRITE |
270                                                         G_PARAM_CONSTRUCT_ONLY |
271                                                         G_PARAM_STATIC_STRINGS));
272 }
273
274 static void
275 g_proxy_address_init (GProxyAddress *proxy)
276 {
277   proxy->priv = g_proxy_address_get_instance_private (proxy);
278   proxy->priv->protocol = NULL;
279   proxy->priv->username = NULL;
280   proxy->priv->password = NULL;
281   proxy->priv->dest_hostname = NULL;
282   proxy->priv->dest_port = 0;
283 }
284
285 /**
286  * g_proxy_address_new:
287  * @inetaddr: The proxy server #GInetAddress.
288  * @port: The proxy server port.
289  * @protocol: The proxy protocol to support, in lower case (e.g. socks, http).
290  * @dest_hostname: The destination hostname the proxy should tunnel to.
291  * @dest_port: The destination port to tunnel to.
292  * @username: (allow-none): The username to authenticate to the proxy server
293  *     (or %NULL).
294  * @password: (allow-none): The password to authenticate to the proxy server
295  *     (or %NULL).
296  *
297  * Creates a new #GProxyAddress for @inetaddr with @protocol that should
298  * tunnel through @dest_hostname and @dest_port.
299  *
300  * (Note that this method doesn't set the #GProxyAddress:uri or
301  * #GProxyAddress:destination-protocol fields; use g_object_new()
302  * directly if you want to set those.)
303  *
304  * Returns: a new #GProxyAddress
305  *
306  * Since: 2.26
307  */
308 GSocketAddress *
309 g_proxy_address_new (GInetAddress  *inetaddr,
310                      guint16        port,
311                      const gchar   *protocol,
312                      const gchar   *dest_hostname,
313                      guint16        dest_port,
314                      const gchar   *username,
315                      const gchar   *password)
316 {
317   return g_object_new (G_TYPE_PROXY_ADDRESS,
318                        "address", inetaddr,
319                        "port", port,
320                        "protocol", protocol,
321                        "destination-hostname", dest_hostname,
322                        "destination-port", dest_port,
323                        "username", username,
324                        "password", password,
325                        NULL);
326 }
327
328
329 /**
330  * g_proxy_address_get_protocol:
331  * @proxy: a #GProxyAddress
332  *
333  * Gets @proxy's protocol. eg, "socks" or "http"
334  *
335  * Returns: the @proxy's protocol
336  *
337  * Since: 2.26
338  */
339 const gchar *
340 g_proxy_address_get_protocol (GProxyAddress *proxy)
341 {
342   return proxy->priv->protocol;
343 }
344
345 /**
346  * g_proxy_address_get_destination_protocol:
347  * @proxy: a #GProxyAddress
348  *
349  * Gets the protocol that is being spoken to the destination
350  * server; eg, "http" or "ftp".
351  *
352  * Returns: the @proxy's destination protocol
353  *
354  * Since: 2.34
355  */
356 const gchar *
357 g_proxy_address_get_destination_protocol (GProxyAddress *proxy)
358 {
359   return proxy->priv->dest_protocol;
360 }
361
362 /**
363  * g_proxy_address_get_destination_hostname:
364  * @proxy: a #GProxyAddress
365  *
366  * Gets @proxy's destination hostname; that is, the name of the host
367  * that will be connected to via the proxy, not the name of the proxy
368  * itself.
369  *
370  * Returns: the @proxy's destination hostname
371  *
372  * Since: 2.26
373  */
374 const gchar *
375 g_proxy_address_get_destination_hostname (GProxyAddress *proxy)
376 {
377   return proxy->priv->dest_hostname;
378 }
379
380 /**
381  * g_proxy_address_get_destination_port:
382  * @proxy: a #GProxyAddress
383  *
384  * Gets @proxy's destination port; that is, the port on the
385  * destination host that will be connected to via the proxy, not the
386  * port number of the proxy itself.
387  *
388  * Returns: the @proxy's destination port
389  *
390  * Since: 2.26
391  */
392 guint16
393 g_proxy_address_get_destination_port (GProxyAddress *proxy)
394 {
395   return proxy->priv->dest_port;
396 }
397
398 /**
399  * g_proxy_address_get_username:
400  * @proxy: a #GProxyAddress
401  *
402  * Gets @proxy's username.
403  *
404  * Returns: the @proxy's username
405  *
406  * Since: 2.26
407  */
408 const gchar *
409 g_proxy_address_get_username (GProxyAddress *proxy)
410 {
411   return proxy->priv->username;
412 }
413
414 /**
415  * g_proxy_address_get_password:
416  * @proxy: a #GProxyAddress
417  *
418  * Gets @proxy's password.
419  *
420  * Returns: the @proxy's password
421  *
422  * Since: 2.26
423  */
424 const gchar *
425 g_proxy_address_get_password (GProxyAddress *proxy)
426 {
427   return proxy->priv->password;
428 }
429
430
431 /**
432  * g_proxy_address_get_uri:
433  * @proxy: a #GProxyAddress
434  *
435  * Gets the proxy URI that @proxy was constructed from.
436  *
437  * Returns: the @proxy's URI, or %NULL if unknown
438  *
439  * Since: 2.34
440  */
441 const gchar *
442 g_proxy_address_get_uri (GProxyAddress *proxy)
443 {
444   return proxy->priv->uri;
445 }