Merge branch '896-variant-type-docs' into 'master'
[glib.git] / gio / gsrvtarget.c
blob6b4068ba4111c6a23832e076145239c82f8561ea
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2008 Red Hat, Inc.
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.
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.
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/>.
21 #include "config.h"
22 #include <glib.h>
23 #include "glibintl.h"
25 #include "gsrvtarget.h"
27 #include <stdlib.h>
28 #include <string.h>
31 /**
32 * SECTION:gsrvtarget
33 * @short_description: DNS SRV record target
34 * @include: gio/gio.h
36 * SRV (service) records are used by some network protocols to provide
37 * service-specific aliasing and load-balancing. For example, XMPP
38 * (Jabber) uses SRV records to locate the XMPP server for a domain;
39 * rather than connecting directly to "example.com" or assuming a
40 * specific server hostname like "xmpp.example.com", an XMPP client
41 * would look up the "xmpp-client" SRV record for "example.com", and
42 * then connect to whatever host was pointed to by that record.
44 * You can use g_resolver_lookup_service() or
45 * g_resolver_lookup_service_async() to find the #GSrvTargets
46 * for a given service. However, if you are simply planning to connect
47 * to the remote service, you can use #GNetworkService's
48 * #GSocketConnectable interface and not need to worry about
49 * #GSrvTarget at all.
52 struct _GSrvTarget {
53 gchar *hostname;
54 guint16 port;
56 guint16 priority;
57 guint16 weight;
60 /**
61 * GSrvTarget:
63 * A single target host/port that a network service is running on.
66 G_DEFINE_BOXED_TYPE (GSrvTarget, g_srv_target,
67 g_srv_target_copy, g_srv_target_free)
69 /**
70 * g_srv_target_new:
71 * @hostname: the host that the service is running on
72 * @port: the port that the service is running on
73 * @priority: the target's priority
74 * @weight: the target's weight
76 * Creates a new #GSrvTarget with the given parameters.
78 * You should not need to use this; normally #GSrvTargets are
79 * created by #GResolver.
81 * Returns: a new #GSrvTarget.
83 * Since: 2.22
85 GSrvTarget *
86 g_srv_target_new (const gchar *hostname,
87 guint16 port,
88 guint16 priority,
89 guint16 weight)
91 GSrvTarget *target = g_slice_new0 (GSrvTarget);
93 target->hostname = g_strdup (hostname);
94 target->port = port;
95 target->priority = priority;
96 target->weight = weight;
98 return target;
102 * g_srv_target_copy:
103 * @target: a #GSrvTarget
105 * Copies @target
107 * Returns: a copy of @target
109 * Since: 2.22
111 GSrvTarget *
112 g_srv_target_copy (GSrvTarget *target)
114 return g_srv_target_new (target->hostname, target->port,
115 target->priority, target->weight);
119 * g_srv_target_free:
120 * @target: a #GSrvTarget
122 * Frees @target
124 * Since: 2.22
126 void
127 g_srv_target_free (GSrvTarget *target)
129 g_free (target->hostname);
130 g_slice_free (GSrvTarget, target);
134 * g_srv_target_get_hostname:
135 * @target: a #GSrvTarget
137 * Gets @target's hostname (in ASCII form; if you are going to present
138 * this to the user, you should use g_hostname_is_ascii_encoded() to
139 * check if it contains encoded Unicode segments, and use
140 * g_hostname_to_unicode() to convert it if it does.)
142 * Returns: @target's hostname
144 * Since: 2.22
146 const gchar *
147 g_srv_target_get_hostname (GSrvTarget *target)
149 return target->hostname;
153 * g_srv_target_get_port:
154 * @target: a #GSrvTarget
156 * Gets @target's port
158 * Returns: @target's port
160 * Since: 2.22
162 guint16
163 g_srv_target_get_port (GSrvTarget *target)
165 return target->port;
169 * g_srv_target_get_priority:
170 * @target: a #GSrvTarget
172 * Gets @target's priority. You should not need to look at this;
173 * #GResolver already sorts the targets according to the algorithm in
174 * RFC 2782.
176 * Returns: @target's priority
178 * Since: 2.22
180 guint16
181 g_srv_target_get_priority (GSrvTarget *target)
183 return target->priority;
187 * g_srv_target_get_weight:
188 * @target: a #GSrvTarget
190 * Gets @target's weight. You should not need to look at this;
191 * #GResolver already sorts the targets according to the algorithm in
192 * RFC 2782.
194 * Returns: @target's weight
196 * Since: 2.22
198 guint16
199 g_srv_target_get_weight (GSrvTarget *target)
201 return target->weight;
204 static gint
205 compare_target (gconstpointer a, gconstpointer b)
207 GSrvTarget *ta = (GSrvTarget *)a;
208 GSrvTarget *tb = (GSrvTarget *)b;
210 if (ta->priority == tb->priority)
212 /* Arrange targets of the same priority "in any order, except
213 * that all those with weight 0 are placed at the beginning of
214 * the list"
216 return ta->weight - tb->weight;
218 else
219 return ta->priority - tb->priority;
223 * g_srv_target_list_sort: (skip)
224 * @targets: a #GList of #GSrvTarget
226 * Sorts @targets in place according to the algorithm in RFC 2782.
228 * Returns: (transfer full): the head of the sorted list.
230 * Since: 2.22
232 GList *
233 g_srv_target_list_sort (GList *targets)
235 gint sum, num, val, priority, weight;
236 GList *t, *out, *tail;
237 GSrvTarget *target;
239 if (!targets)
240 return NULL;
242 if (!targets->next)
244 target = targets->data;
245 if (!strcmp (target->hostname, "."))
247 /* 'A Target of "." means that the service is decidedly not
248 * available at this domain.'
250 g_srv_target_free (target);
251 g_list_free (targets);
252 return NULL;
256 /* Sort input list by priority, and put the 0-weight targets first
257 * in each priority group. Initialize output list to %NULL.
259 targets = g_list_sort (targets, compare_target);
260 out = tail = NULL;
262 /* For each group of targets with the same priority, remove them
263 * from @targets and append them to @out in a valid order.
265 while (targets)
267 priority = ((GSrvTarget *)targets->data)->priority;
269 /* Count the number of targets at this priority level, and
270 * compute the sum of their weights.
272 sum = num = 0;
273 for (t = targets; t; t = t->next)
275 target = (GSrvTarget *)t->data;
276 if (target->priority != priority)
277 break;
278 sum += target->weight;
279 num++;
282 /* While there are still targets at this priority level... */
283 while (num)
285 /* Randomly select from the targets at this priority level,
286 * giving precedence to the ones with higher weight,
287 * according to the rules from RFC 2782.
289 val = g_random_int_range (0, sum + 1);
290 for (t = targets; ; t = t->next)
292 weight = ((GSrvTarget *)t->data)->weight;
293 if (weight >= val)
294 break;
295 val -= weight;
298 targets = g_list_remove_link (targets, t);
300 if (!out)
301 out = t;
302 else
303 tail->next = t;
304 tail = t;
306 sum -= weight;
307 num--;
311 return out;