g_unix_set_fd_nonblocking: New API to control file descriptor blocking state
[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: (allow-none): The username to authenticate to the proxy server
237  *     (or %NULL).
238  * @password: (allow-none): The password to authenticate to the proxy server
239  *     (or %NULL).
240  *
241  * Creates a new #GProxyAddress for @inetaddr with @protocol that should
242  * tunnel through @dest_hostname and @dest_port.
243  *
244  * Returns: a new #GProxyAddress
245  *
246  * Since: 2.26
247  */
248 GSocketAddress *
249 g_proxy_address_new (GInetAddress  *inetaddr,
250                      guint16        port,
251                      const gchar   *protocol,
252                      const gchar   *dest_hostname,
253                      guint16        dest_port,
254                      const gchar   *username,
255                      const gchar   *password)
256 {
257   return g_object_new (G_TYPE_PROXY_ADDRESS,
258                        "address", inetaddr,
259                        "port", port,
260                        "protocol", protocol,
261                        "destination-hostname", dest_hostname,
262                        "destination-port", dest_port,
263                        "username", username,
264                        "password", password,
265                        NULL);
266 }
267
268
269 /**
270  * g_proxy_address_get_protocol:
271  * @proxy: a #GProxyAddress
272  *
273  * Gets @proxy's protocol.
274  *
275  * Returns: the @proxy's protocol
276  *
277  * Since: 2.26
278  */
279 const gchar *
280 g_proxy_address_get_protocol (GProxyAddress *proxy)
281 {
282   return proxy->priv->protocol;
283 }
284
285 /**
286  * g_proxy_address_get_destination_hostname
287  * @proxy: a #GProxyAddress
288  *
289  * Gets @proxy's destination hostname.
290  *
291  * Returns: the @proxy's destination hostname
292  *
293  * Since: 2.26
294  */
295 const gchar *
296 g_proxy_address_get_destination_hostname (GProxyAddress *proxy)
297 {
298   return proxy->priv->dest_hostname;
299 }
300
301 /**
302  * g_proxy_address_get_destination_port
303  * @proxy: a #GProxyAddress
304  *
305  * Gets @proxy's destination port.
306  *
307  * Returns: the @proxy's destination port
308  *
309  * Since: 2.26
310  */
311 guint16
312 g_proxy_address_get_destination_port (GProxyAddress *proxy)
313 {
314   return proxy->priv->dest_port;
315 }
316
317 /**
318  * g_proxy_address_get_username
319  * @proxy: a #GProxyAddress
320  *
321  * Gets @proxy's username.
322  *
323  * Returns: the @proxy's username
324  *
325  * Since: 2.26
326  */
327 const gchar *
328 g_proxy_address_get_username (GProxyAddress *proxy)
329 {
330   return proxy->priv->username;
331 }
332
333 /**
334  * g_proxy_address_get_password
335  * @proxy: a #GProxyAddress
336  *
337  * Gets @proxy's password.
338  *
339  * Returns: the @proxy's password
340  *
341  * Since: 2.26
342  */
343 const gchar *
344 g_proxy_address_get_password (GProxyAddress *proxy)
345 {
346   return proxy->priv->password;
347 }