61dfc7d08abd3a381579c5363f4c1824a717a5d0
[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 G_DEFINE_TYPE (GProxyAddress, g_proxy_address, G_TYPE_INET_SOCKET_ADDRESS);
47
48 enum
49 {
50   PROP_0,
51   PROP_PROTOCOL,
52   PROP_DESTINATION_HOSTNAME,
53   PROP_DESTINATION_PORT,
54   PROP_USERNAME,
55   PROP_PASSWORD
56 };
57
58 struct _GProxyAddressPrivate
59 {
60   gchar          *protocol;
61   gchar          *username;
62   gchar          *password;
63   gchar          *dest_hostname;
64   guint16         dest_port;
65 };
66
67 static void
68 g_proxy_address_finalize (GObject *object)
69 {
70   GProxyAddress *proxy = G_PROXY_ADDRESS (object);
71
72   g_free (proxy->priv->protocol);
73   g_free (proxy->priv->username);
74   g_free (proxy->priv->password);
75   g_free (proxy->priv->dest_hostname);
76
77   G_OBJECT_CLASS (g_proxy_address_parent_class)->finalize (object);
78 }
79
80 static void
81 g_proxy_address_set_property (GObject      *object,
82                              guint         prop_id,
83                              const GValue *value,
84                              GParamSpec   *pspec)
85 {
86   GProxyAddress *proxy = G_PROXY_ADDRESS (object);
87
88   switch (prop_id)
89     {
90     case PROP_PROTOCOL:
91       g_free (proxy->priv->protocol);
92       proxy->priv->protocol = g_value_dup_string (value);
93       break;
94
95     case PROP_DESTINATION_HOSTNAME:
96       g_free (proxy->priv->dest_hostname);
97       proxy->priv->dest_hostname = g_value_dup_string (value);
98       break;
99
100     case PROP_DESTINATION_PORT:
101       proxy->priv->dest_port = g_value_get_uint (value);
102       break;
103
104     case PROP_USERNAME:
105       g_free (proxy->priv->username);
106       proxy->priv->username = g_value_dup_string (value);
107       break;
108
109     case PROP_PASSWORD:
110       g_free (proxy->priv->password);
111       proxy->priv->password = g_value_dup_string (value);
112       break;
113
114     default:
115       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116     }
117 }
118
119 static void
120 g_proxy_address_get_property (GObject    *object,
121                               guint       prop_id,
122                               GValue     *value,
123                               GParamSpec *pspec)
124 {
125   GProxyAddress *proxy = G_PROXY_ADDRESS (object);
126
127   switch (prop_id)
128     {
129       case PROP_PROTOCOL:
130         g_value_set_string (value, proxy->priv->protocol);
131         break;
132
133       case PROP_DESTINATION_HOSTNAME:
134         g_value_set_string (value, proxy->priv->dest_hostname);
135         break;
136
137       case PROP_DESTINATION_PORT:
138         g_value_set_uint (value, proxy->priv->dest_port);
139         break;
140
141       case PROP_USERNAME:
142         g_value_set_string (value, proxy->priv->username);
143         break;
144
145       case PROP_PASSWORD:
146         g_value_set_string (value, proxy->priv->password);
147         break;
148
149       default:
150         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151     }
152 }
153
154 static void
155 g_proxy_address_class_init (GProxyAddressClass *klass)
156 {
157   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
158
159   g_type_class_add_private (klass, sizeof (GProxyAddressPrivate));
160
161   gobject_class->finalize = g_proxy_address_finalize;
162   gobject_class->set_property = g_proxy_address_set_property;
163   gobject_class->get_property = g_proxy_address_get_property;
164
165   g_object_class_install_property (gobject_class,
166                                    PROP_PROTOCOL,
167                                    g_param_spec_string ("protocol",
168                                                        P_("Protocol"),
169                                                        P_("The proxy protocol"),
170                                                        NULL,
171                                                        G_PARAM_READWRITE |
172                                                        G_PARAM_CONSTRUCT_ONLY |
173                                                        G_PARAM_STATIC_STRINGS));
174
175   g_object_class_install_property (gobject_class,
176                                    PROP_USERNAME,
177                                    g_param_spec_string ("username",
178                                                        P_("Username"),
179                                                        P_("The proxy username"),
180                                                        NULL,
181                                                        G_PARAM_READWRITE |
182                                                        G_PARAM_CONSTRUCT_ONLY |
183                                                        G_PARAM_STATIC_STRINGS));
184
185   g_object_class_install_property (gobject_class,
186                                    PROP_PASSWORD,
187                                    g_param_spec_string ("password",
188                                                        P_("Password"),
189                                                        P_("The proxy password"),
190                                                        NULL,
191                                                        G_PARAM_READWRITE |
192                                                        G_PARAM_CONSTRUCT_ONLY |
193                                                        G_PARAM_STATIC_STRINGS));
194
195   g_object_class_install_property (gobject_class,
196                                    PROP_DESTINATION_HOSTNAME,
197                                    g_param_spec_string ("destination-hostname",
198                                                        P_("Destination Hostname"),
199                                                        P_("The proxy destination hostname"),
200                                                        NULL,
201                                                        G_PARAM_READWRITE |
202                                                        G_PARAM_CONSTRUCT_ONLY |
203                                                        G_PARAM_STATIC_STRINGS));
204
205   g_object_class_install_property (gobject_class,
206                                    PROP_DESTINATION_PORT,
207                                    g_param_spec_uint ("destination-port",
208                                                       P_("Destination Port"),
209                                                       P_("The proxy destination port"),
210                                                       0, 65535, 0,
211                                                       G_PARAM_READWRITE |
212                                                       G_PARAM_CONSTRUCT_ONLY |
213                                                       G_PARAM_STATIC_STRINGS));
214 }
215
216 static void
217 g_proxy_address_init (GProxyAddress *proxy)
218 {
219   proxy->priv = G_TYPE_INSTANCE_GET_PRIVATE (proxy,
220                                              G_TYPE_PROXY_ADDRESS,
221                                              GProxyAddressPrivate);
222   proxy->priv->protocol = NULL;
223   proxy->priv->username = NULL;
224   proxy->priv->password = NULL;
225   proxy->priv->dest_hostname = NULL;
226   proxy->priv->dest_port = 0;
227 }
228
229 /**
230  * g_proxy_address_new:
231  * @inetaddr: The proxy server #GInetAddress.
232  * @port: The proxy server port.
233  * @protocol: The proxy protocol to support, in lower case (e.g. socks, http).
234  * @dest_hostname: The destination hostname the the proxy should tunnel to.
235  * @dest_port: The destination port to tunnel to.
236  * @username: The username to authenticate to the proxy server (or %NULL).
237  * @password: The password to authenticate to the proxy server (or %NULL).
238  *
239  * Creates a new #GProxyAddress for @inetaddr with @protocol that should
240  * tunnel through @dest_hostname and @dest_port.
241  *
242  * Returns: a new #GProxyAddress
243  *
244  * Since: 2.26
245  */
246 GSocketAddress *
247 g_proxy_address_new (GInetAddress  *inetaddr,
248                      guint16        port,
249                      const gchar   *protocol,
250                      const gchar   *dest_hostname,
251                      guint16        dest_port,
252                      const gchar   *username,
253                      const gchar   *password)
254 {
255   return g_object_new (G_TYPE_PROXY_ADDRESS,
256                        "address", inetaddr,
257                        "port", port,
258                        "protocol", protocol,
259                        "destination-hostname", dest_hostname,
260                        "destination-port", dest_port,
261                        "username", username,
262                        "password", password,
263                        NULL);
264 }
265
266
267 /**
268  * g_proxy_address_get_protocol:
269  * @proxy: a #GProxyAddress
270  *
271  * Gets @proxy's protocol.
272  *
273  * Returns: the @proxy's protocol
274  *
275  * Since: 2.26
276  */
277 const gchar *
278 g_proxy_address_get_protocol (GProxyAddress *proxy)
279 {
280   return proxy->priv->protocol;
281 }
282
283 /**
284  * g_proxy_address_get_destination_hostname
285  * @proxy: a #GProxyAddress
286  *
287  * Gets @proxy's destination hostname.
288  *
289  * Returns: the @proxy's destination hostname
290  *
291  * Since: 2.26
292  */
293 const gchar *
294 g_proxy_address_get_destination_hostname (GProxyAddress *proxy)
295 {
296   return proxy->priv->dest_hostname;
297 }
298
299 /**
300  * g_proxy_address_get_destination_port
301  * @proxy: a #GProxyAddress
302  *
303  * Gets @proxy's destination port.
304  *
305  * Returns: the @proxy's destination port
306  *
307  * Since: 2.26
308  */
309 guint16
310 g_proxy_address_get_destination_port (GProxyAddress *proxy)
311 {
312   return proxy->priv->dest_port;
313 }
314
315 /**
316  * g_proxy_address_get_username
317  * @proxy: a #GProxyAddress
318  *
319  * Gets @proxy's username.
320  *
321  * Returns: the @proxy's username
322  *
323  * Since: 2.26
324  */
325 const gchar *
326 g_proxy_address_get_username (GProxyAddress *proxy)
327 {
328   return proxy->priv->username;
329 }
330
331 /**
332  * g_proxy_address_get_password
333  * @proxy: a #GProxyAddress
334  *
335  * Gets @proxy's password.
336  *
337  * Returns: the @proxy's password
338  *
339  * Since: 2.26
340  */
341 const gchar *
342 g_proxy_address_get_password (GProxyAddress *proxy)
343 {
344   return proxy->priv->password;
345 }