Coverage Report

Created: 2025-07-01 07:09

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