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