Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gsocketconnectable.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 * 
3
 * Copyright (C) 2008 Red Hat, Inc.
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
21
#include "config.h"
22
#include "gsocketconnectable.h"
23
#include "glibintl.h"
24
25
26
/**
27
 * SECTION:gsocketconnectable
28
 * @short_description: Interface for potential socket endpoints
29
 * @include: gio/gio.h
30
 *
31
 * Objects that describe one or more potential socket endpoints
32
 * implement #GSocketConnectable. Callers can then use
33
 * g_socket_connectable_enumerate() to get a #GSocketAddressEnumerator
34
 * to try out each socket address in turn until one succeeds, as shown
35
 * in the sample code below.
36
 *
37
 * |[<!-- language="C" -->
38
 * MyConnectionType *
39
 * connect_to_host (const char    *hostname,
40
 *                  guint16        port,
41
 *                  GCancellable  *cancellable,
42
 *                  GError       **error)
43
 * {
44
 *   MyConnection *conn = NULL;
45
 *   GSocketConnectable *addr;
46
 *   GSocketAddressEnumerator *enumerator;
47
 *   GSocketAddress *sockaddr;
48
 *   GError *conn_error = NULL;
49
 *
50
 *   addr = g_network_address_new (hostname, port);
51
 *   enumerator = g_socket_connectable_enumerate (addr);
52
 *   g_object_unref (addr);
53
 *
54
 *   // Try each sockaddr until we succeed. Record the first connection error,
55
 *   // but not any further ones (since they'll probably be basically the same
56
 *   // as the first).
57
 *   while (!conn && (sockaddr = g_socket_address_enumerator_next (enumerator, cancellable, error))
58
 *     {
59
 *       conn = connect_to_sockaddr (sockaddr, conn_error ? NULL : &conn_error);
60
 *       g_object_unref (sockaddr);
61
 *     }
62
 *   g_object_unref (enumerator);
63
 *
64
 *   if (conn)
65
 *     {
66
 *       if (conn_error)
67
 *         {
68
 *           // We couldn't connect to the first address, but we succeeded
69
 *           // in connecting to a later address.
70
 *           g_error_free (conn_error);
71
 *         }
72
 *       return conn;
73
 *     }
74
 *   else if (error)
75
 *     {
76
 *       /// Either initial lookup failed, or else the caller cancelled us.
77
 *       if (conn_error)
78
 *         g_error_free (conn_error);
79
 *       return NULL;
80
 *     }
81
 *   else
82
 *     {
83
 *       g_error_propagate (error, conn_error);
84
 *       return NULL;
85
 *     }
86
 * }
87
 * ]|
88
 */
89
90
91
typedef GSocketConnectableIface GSocketConnectableInterface;
92
G_DEFINE_INTERFACE (GSocketConnectable, g_socket_connectable, G_TYPE_OBJECT)
93
94
static void
95
g_socket_connectable_default_init (GSocketConnectableInterface *iface)
96
0
{
97
0
}
98
99
/**
100
 * g_socket_connectable_enumerate:
101
 * @connectable: a #GSocketConnectable
102
 *
103
 * Creates a #GSocketAddressEnumerator for @connectable.
104
 *
105
 * Returns: (transfer full): a new #GSocketAddressEnumerator.
106
 *
107
 * Since: 2.22
108
 */
109
GSocketAddressEnumerator *
110
g_socket_connectable_enumerate (GSocketConnectable *connectable)
111
0
{
112
0
  GSocketConnectableIface *iface;
113
114
0
  g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
115
116
0
  iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
117
118
0
  return (* iface->enumerate) (connectable);
119
0
}
120
121
/**
122
 * g_socket_connectable_proxy_enumerate:
123
 * @connectable: a #GSocketConnectable
124
 *
125
 * Creates a #GSocketAddressEnumerator for @connectable that will
126
 * return a #GProxyAddress for each of its addresses that you must connect
127
 * to via a proxy.
128
 *
129
 * If @connectable does not implement
130
 * g_socket_connectable_proxy_enumerate(), this will fall back to
131
 * calling g_socket_connectable_enumerate().
132
 *
133
 * Returns: (transfer full): a new #GSocketAddressEnumerator.
134
 *
135
 * Since: 2.26
136
 */
137
GSocketAddressEnumerator *
138
g_socket_connectable_proxy_enumerate (GSocketConnectable *connectable)
139
0
{
140
0
  GSocketConnectableIface *iface;
141
142
0
  g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
143
144
0
  iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
145
146
0
  if (iface->proxy_enumerate)
147
0
    return (* iface->proxy_enumerate) (connectable);
148
0
  else
149
0
    return (* iface->enumerate) (connectable);
150
0
}
151
152
/**
153
 * g_socket_connectable_to_string:
154
 * @connectable: a #GSocketConnectable
155
 *
156
 * Format a #GSocketConnectable as a string. This is a human-readable format for
157
 * use in debugging output, and is not a stable serialization format. It is not
158
 * suitable for use in user interfaces as it exposes too much information for a
159
 * user.
160
 *
161
 * If the #GSocketConnectable implementation does not support string formatting,
162
 * the implementation’s type name will be returned as a fallback.
163
 *
164
 * Returns: (transfer full): the formatted string
165
 *
166
 * Since: 2.48
167
 */
168
gchar *
169
g_socket_connectable_to_string (GSocketConnectable *connectable)
170
0
{
171
0
  GSocketConnectableIface *iface;
172
173
0
  g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
174
175
0
  iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
176
177
0
  if (iface->to_string != NULL)
178
0
    return iface->to_string (connectable);
179
0
  else
180
0
    return g_strdup (G_OBJECT_TYPE_NAME (connectable));
181
0
}