Coverage Report

Created: 2025-06-13 06:55

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