Check for overflow when allocating RcBox
[glib.git] / gio / gproxyaddress.c
blob0b78caa912daa4703905c88e799f963be54499ff
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Collabora, Ltd.
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.1 of the License, or (at your option) any later version.
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.
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/>.
18 * Authors: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
21 #include <config.h>
22 #include <glib.h>
23 #include <string.h>
25 #include <gio/gsocketaddress.h>
27 #include "gproxyaddress.h"
28 #include "glibintl.h"
30 /**
31 * SECTION:gproxyaddress
32 * @short_description: An internet address with proxy information
33 * @include: gio/gio.h
35 * Support for proxied #GInetSocketAddress.
38 /**
39 * GProxyAddress:
41 * A #GInetSocketAddress representing a connection via a proxy server
43 * Since: 2.26
44 **/
46 /**
47 * GProxyAddressClass:
49 * Class structure for #GProxyAddress.
51 * Since: 2.26
52 **/
54 enum
56 PROP_0,
57 PROP_PROTOCOL,
58 PROP_DESTINATION_PROTOCOL,
59 PROP_DESTINATION_HOSTNAME,
60 PROP_DESTINATION_PORT,
61 PROP_USERNAME,
62 PROP_PASSWORD,
63 PROP_URI
66 struct _GProxyAddressPrivate
68 gchar *uri;
69 gchar *protocol;
70 gchar *username;
71 gchar *password;
72 gchar *dest_protocol;
73 gchar *dest_hostname;
74 guint16 dest_port;
77 G_DEFINE_TYPE_WITH_PRIVATE (GProxyAddress, g_proxy_address, G_TYPE_INET_SOCKET_ADDRESS)
79 static void
80 g_proxy_address_finalize (GObject *object)
82 GProxyAddress *proxy = G_PROXY_ADDRESS (object);
84 g_free (proxy->priv->uri);
85 g_free (proxy->priv->protocol);
86 g_free (proxy->priv->username);
87 g_free (proxy->priv->password);
88 g_free (proxy->priv->dest_hostname);
89 g_free (proxy->priv->dest_protocol);
91 G_OBJECT_CLASS (g_proxy_address_parent_class)->finalize (object);
94 static void
95 g_proxy_address_set_property (GObject *object,
96 guint prop_id,
97 const GValue *value,
98 GParamSpec *pspec)
100 GProxyAddress *proxy = G_PROXY_ADDRESS (object);
102 switch (prop_id)
104 case PROP_PROTOCOL:
105 g_free (proxy->priv->protocol);
106 proxy->priv->protocol = g_value_dup_string (value);
107 break;
109 case PROP_DESTINATION_PROTOCOL:
110 g_free (proxy->priv->dest_protocol);
111 proxy->priv->dest_protocol = g_value_dup_string (value);
112 break;
114 case PROP_DESTINATION_HOSTNAME:
115 g_free (proxy->priv->dest_hostname);
116 proxy->priv->dest_hostname = g_value_dup_string (value);
117 break;
119 case PROP_DESTINATION_PORT:
120 proxy->priv->dest_port = g_value_get_uint (value);
121 break;
123 case PROP_USERNAME:
124 g_free (proxy->priv->username);
125 proxy->priv->username = g_value_dup_string (value);
126 break;
128 case PROP_PASSWORD:
129 g_free (proxy->priv->password);
130 proxy->priv->password = g_value_dup_string (value);
131 break;
133 case PROP_URI:
134 g_free (proxy->priv->uri);
135 proxy->priv->uri = g_value_dup_string (value);
136 break;
138 default:
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143 static void
144 g_proxy_address_get_property (GObject *object,
145 guint prop_id,
146 GValue *value,
147 GParamSpec *pspec)
149 GProxyAddress *proxy = G_PROXY_ADDRESS (object);
151 switch (prop_id)
153 case PROP_PROTOCOL:
154 g_value_set_string (value, proxy->priv->protocol);
155 break;
157 case PROP_DESTINATION_PROTOCOL:
158 g_value_set_string (value, proxy->priv->dest_protocol);
159 break;
161 case PROP_DESTINATION_HOSTNAME:
162 g_value_set_string (value, proxy->priv->dest_hostname);
163 break;
165 case PROP_DESTINATION_PORT:
166 g_value_set_uint (value, proxy->priv->dest_port);
167 break;
169 case PROP_USERNAME:
170 g_value_set_string (value, proxy->priv->username);
171 break;
173 case PROP_PASSWORD:
174 g_value_set_string (value, proxy->priv->password);
175 break;
177 case PROP_URI:
178 g_value_set_string (value, proxy->priv->uri);
179 break;
181 default:
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186 static void
187 g_proxy_address_class_init (GProxyAddressClass *klass)
189 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
191 gobject_class->finalize = g_proxy_address_finalize;
192 gobject_class->set_property = g_proxy_address_set_property;
193 gobject_class->get_property = g_proxy_address_get_property;
195 g_object_class_install_property (gobject_class,
196 PROP_PROTOCOL,
197 g_param_spec_string ("protocol",
198 P_("Protocol"),
199 P_("The proxy protocol"),
200 NULL,
201 G_PARAM_READWRITE |
202 G_PARAM_CONSTRUCT_ONLY |
203 G_PARAM_STATIC_STRINGS));
205 g_object_class_install_property (gobject_class,
206 PROP_USERNAME,
207 g_param_spec_string ("username",
208 P_("Username"),
209 P_("The proxy username"),
210 NULL,
211 G_PARAM_READWRITE |
212 G_PARAM_CONSTRUCT_ONLY |
213 G_PARAM_STATIC_STRINGS));
215 g_object_class_install_property (gobject_class,
216 PROP_PASSWORD,
217 g_param_spec_string ("password",
218 P_("Password"),
219 P_("The proxy password"),
220 NULL,
221 G_PARAM_READWRITE |
222 G_PARAM_CONSTRUCT_ONLY |
223 G_PARAM_STATIC_STRINGS));
226 * GProxyAddress:destination-protocol:
228 * The protocol being spoke to the destination host, or %NULL if
229 * the #GProxyAddress doesn't know.
231 * Since: 2.34
233 g_object_class_install_property (gobject_class,
234 PROP_DESTINATION_PROTOCOL,
235 g_param_spec_string ("destination-protocol",
236 P_("Destionation Protocol"),
237 P_("The proxy destination protocol"),
238 NULL,
239 G_PARAM_READWRITE |
240 G_PARAM_CONSTRUCT_ONLY |
241 G_PARAM_STATIC_STRINGS));
243 g_object_class_install_property (gobject_class,
244 PROP_DESTINATION_HOSTNAME,
245 g_param_spec_string ("destination-hostname",
246 P_("Destination Hostname"),
247 P_("The proxy destination hostname"),
248 NULL,
249 G_PARAM_READWRITE |
250 G_PARAM_CONSTRUCT_ONLY |
251 G_PARAM_STATIC_STRINGS));
253 g_object_class_install_property (gobject_class,
254 PROP_DESTINATION_PORT,
255 g_param_spec_uint ("destination-port",
256 P_("Destination Port"),
257 P_("The proxy destination port"),
258 0, 65535, 0,
259 G_PARAM_READWRITE |
260 G_PARAM_CONSTRUCT_ONLY |
261 G_PARAM_STATIC_STRINGS));
264 * GProxyAddress:uri:
266 * The URI string that the proxy was constructed from (or %NULL
267 * if the creator didn't specify this).
269 * Since: 2.34
271 g_object_class_install_property (gobject_class,
272 PROP_URI,
273 g_param_spec_string ("uri",
274 P_("URI"),
275 P_("The proxy’s URI"),
276 NULL,
277 G_PARAM_READWRITE |
278 G_PARAM_CONSTRUCT_ONLY |
279 G_PARAM_STATIC_STRINGS));
282 static void
283 g_proxy_address_init (GProxyAddress *proxy)
285 proxy->priv = g_proxy_address_get_instance_private (proxy);
286 proxy->priv->protocol = NULL;
287 proxy->priv->username = NULL;
288 proxy->priv->password = NULL;
289 proxy->priv->dest_hostname = NULL;
290 proxy->priv->dest_port = 0;
294 * g_proxy_address_new:
295 * @inetaddr: The proxy server #GInetAddress.
296 * @port: The proxy server port.
297 * @protocol: The proxy protocol to support, in lower case (e.g. socks, http).
298 * @dest_hostname: The destination hostname the proxy should tunnel to.
299 * @dest_port: The destination port to tunnel to.
300 * @username: (nullable): The username to authenticate to the proxy server
301 * (or %NULL).
302 * @password: (nullable): The password to authenticate to the proxy server
303 * (or %NULL).
305 * Creates a new #GProxyAddress for @inetaddr with @protocol that should
306 * tunnel through @dest_hostname and @dest_port.
308 * (Note that this method doesn't set the #GProxyAddress:uri or
309 * #GProxyAddress:destination-protocol fields; use g_object_new()
310 * directly if you want to set those.)
312 * Returns: a new #GProxyAddress
314 * Since: 2.26
316 GSocketAddress *
317 g_proxy_address_new (GInetAddress *inetaddr,
318 guint16 port,
319 const gchar *protocol,
320 const gchar *dest_hostname,
321 guint16 dest_port,
322 const gchar *username,
323 const gchar *password)
325 return g_object_new (G_TYPE_PROXY_ADDRESS,
326 "address", inetaddr,
327 "port", port,
328 "protocol", protocol,
329 "destination-hostname", dest_hostname,
330 "destination-port", dest_port,
331 "username", username,
332 "password", password,
333 NULL);
338 * g_proxy_address_get_protocol:
339 * @proxy: a #GProxyAddress
341 * Gets @proxy's protocol. eg, "socks" or "http"
343 * Returns: the @proxy's protocol
345 * Since: 2.26
347 const gchar *
348 g_proxy_address_get_protocol (GProxyAddress *proxy)
350 return proxy->priv->protocol;
354 * g_proxy_address_get_destination_protocol:
355 * @proxy: a #GProxyAddress
357 * Gets the protocol that is being spoken to the destination
358 * server; eg, "http" or "ftp".
360 * Returns: the @proxy's destination protocol
362 * Since: 2.34
364 const gchar *
365 g_proxy_address_get_destination_protocol (GProxyAddress *proxy)
367 return proxy->priv->dest_protocol;
371 * g_proxy_address_get_destination_hostname:
372 * @proxy: a #GProxyAddress
374 * Gets @proxy's destination hostname; that is, the name of the host
375 * that will be connected to via the proxy, not the name of the proxy
376 * itself.
378 * Returns: the @proxy's destination hostname
380 * Since: 2.26
382 const gchar *
383 g_proxy_address_get_destination_hostname (GProxyAddress *proxy)
385 return proxy->priv->dest_hostname;
389 * g_proxy_address_get_destination_port:
390 * @proxy: a #GProxyAddress
392 * Gets @proxy's destination port; that is, the port on the
393 * destination host that will be connected to via the proxy, not the
394 * port number of the proxy itself.
396 * Returns: the @proxy's destination port
398 * Since: 2.26
400 guint16
401 g_proxy_address_get_destination_port (GProxyAddress *proxy)
403 return proxy->priv->dest_port;
407 * g_proxy_address_get_username:
408 * @proxy: a #GProxyAddress
410 * Gets @proxy's username.
412 * Returns: the @proxy's username
414 * Since: 2.26
416 const gchar *
417 g_proxy_address_get_username (GProxyAddress *proxy)
419 return proxy->priv->username;
423 * g_proxy_address_get_password:
424 * @proxy: a #GProxyAddress
426 * Gets @proxy's password.
428 * Returns: the @proxy's password
430 * Since: 2.26
432 const gchar *
433 g_proxy_address_get_password (GProxyAddress *proxy)
435 return proxy->priv->password;
440 * g_proxy_address_get_uri:
441 * @proxy: a #GProxyAddress
443 * Gets the proxy URI that @proxy was constructed from.
445 * Returns: the @proxy's URI, or %NULL if unknown
447 * Since: 2.34
449 const gchar *
450 g_proxy_address_get_uri (GProxyAddress *proxy)
452 return proxy->priv->uri;