2 * Copyright (C) 2012 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 #include "rtsp-address-pool.h"
25 GST_DEBUG_CATEGORY_STATIC (rtsp_address_pool_debug);
26 #define GST_CAT_DEFAULT rtsp_address_pool_debug
28 #define GST_RTSP_ADDRESS_POOL_GET_PRIVATE(obj) \
29 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_ADDRESS_POOL, GstRTSPAddressPoolPrivate))
31 struct _GstRTSPAddressPoolPrivate
38 #define ADDR_IS_IPV4(a) ((a)->size == 4)
39 #define ADDR_IS_IPV6(a) ((a)->size == 16)
40 #define ADDR_IS_EVEN_PORT(a) (((a)->port & 1) == 0)
56 #define RANGE_IS_SINGLE(r) (memcmp ((r)->min.bytes, (r)->max.bytes, (r)->min.size) == 0)
58 #define gst_rtsp_address_pool_parent_class parent_class
59 G_DEFINE_TYPE (GstRTSPAddressPool, gst_rtsp_address_pool, G_TYPE_OBJECT);
61 static void gst_rtsp_address_pool_finalize (GObject * obj);
64 gst_rtsp_address_pool_class_init (GstRTSPAddressPoolClass * klass)
66 GObjectClass *gobject_class;
68 gobject_class = G_OBJECT_CLASS (klass);
70 gobject_class->finalize = gst_rtsp_address_pool_finalize;
72 g_type_class_add_private (klass, sizeof (GstRTSPAddressPoolPrivate));
74 GST_DEBUG_CATEGORY_INIT (rtsp_address_pool_debug, "rtspaddresspool", 0,
75 "GstRTSPAddressPool");
79 gst_rtsp_address_pool_init (GstRTSPAddressPool * pool)
81 pool->priv = GST_RTSP_ADDRESS_POOL_GET_PRIVATE (pool);
83 g_mutex_init (&pool->priv->lock);
87 free_range (AddrRange * range)
89 g_slice_free (AddrRange, range);
93 gst_rtsp_address_pool_finalize (GObject * obj)
95 GstRTSPAddressPool *pool;
97 pool = GST_RTSP_ADDRESS_POOL (obj);
99 g_list_free_full (pool->priv->addresses, (GDestroyNotify) free_range);
100 g_list_free_full (pool->priv->allocated, (GDestroyNotify) free_range);
101 g_mutex_clear (&pool->priv->lock);
103 G_OBJECT_CLASS (parent_class)->finalize (obj);
107 * gst_rtsp_address_pool_new:
109 * Make a new #GstRTSPAddressPool.
111 * Returns: a new #GstRTSPAddressPool
114 gst_rtsp_address_pool_new (void)
116 GstRTSPAddressPool *pool;
118 pool = g_object_new (GST_TYPE_RTSP_ADDRESS_POOL, NULL);
124 fill_address (const gchar * address, guint16 port, Addr * addr)
128 inet = g_inet_address_new_from_string (address);
132 addr->size = g_inet_address_get_native_size (inet);
133 memcpy (addr->bytes, g_inet_address_to_bytes (inet), addr->size);
134 g_object_unref (inet);
141 get_address_string (Addr * addr)
146 inet = g_inet_address_new_from_bytes (addr->bytes,
147 addr->size == 4 ? G_SOCKET_FAMILY_IPV4 : G_SOCKET_FAMILY_IPV6);
148 res = g_inet_address_to_string (inet);
149 g_object_unref (inet);
155 * gst_rtsp_address_pool_add_range:
156 * @pool: a #GstRTSPAddressPool
157 * @min_address: a minimum address to add
158 * @max_address: a maximum address to add
159 * @min_port: the minimum port
160 * @max_port: the maximum port
163 * Adds the multicast addresses from @min_addess to @max_address (inclusive)
164 * to @pool. The valid port range for the addresses will be from @min_port to
165 * @max_port inclusive.
167 * Returns: %TRUE if the addresses could be added.
170 gst_rtsp_address_pool_add_range (GstRTSPAddressPool * pool,
171 const gchar * min_address, const gchar * max_address,
172 guint16 min_port, guint16 max_port, guint8 ttl)
175 GstRTSPAddressPoolPrivate *priv;
177 g_return_val_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool), FALSE);
178 g_return_val_if_fail (min_port <= max_port, FALSE);
182 range = g_slice_new0 (AddrRange);
184 if (!fill_address (min_address, min_port, &range->min))
186 if (!fill_address (max_address, max_port, &range->max))
189 if (range->min.size != range->max.size)
191 if (memcmp (range->min.bytes, range->max.bytes, range->min.size) > 0)
196 GST_DEBUG_OBJECT (pool, "adding %s-%s:%u-%u ttl %u", min_address, max_address,
197 min_port, max_port, ttl);
199 g_mutex_lock (&priv->lock);
200 priv->addresses = g_list_prepend (priv->addresses, range);
201 g_mutex_unlock (&priv->lock);
208 GST_ERROR_OBJECT (pool, "invalid address range %s-%s", min_address,
210 g_slice_free (AddrRange, range);
216 inc_address (GstRTSPAddressPool * pool, Addr * addr)
222 for (i = addr->size - 1; i >= 0 && carry > 0; i--) {
223 carry += addr->bytes[i];
224 addr->bytes[i] = carry & 0xff;
230 split_range (GstRTSPAddressPool * pool, AddrRange * range, gint skip,
233 GstRTSPAddressPoolPrivate *priv = pool->priv;
236 if (!RANGE_IS_SINGLE (range)) {
237 /* min and max are not the same, we have more than one address. */
238 temp = g_slice_dup (AddrRange, range);
239 /* increment the range min address */
240 inc_address (pool, &temp->min);
241 /* and store back in pool */
242 priv->addresses = g_list_prepend (priv->addresses, temp);
244 /* adjust range with only the first address */
245 memcpy (range->max.bytes, range->min.bytes, range->min.size);
248 /* range now contains only one single address */
250 /* make a range with the skipped ports */
251 temp = g_slice_dup (AddrRange, range);
252 temp->max.port = temp->min.port + skip;
253 /* and store back in pool */
254 priv->addresses = g_list_prepend (priv->addresses, temp);
256 /* increment range port */
257 range->min.port += skip;
259 /* range now contains single address with desired port number */
260 if (range->max.port - range->min.port + 1 > n_ports) {
261 /* make a range with the remaining ports */
262 temp = g_slice_dup (AddrRange, range);
263 temp->min.port += n_ports;
264 /* and store back in pool */
265 priv->addresses = g_list_prepend (priv->addresses, temp);
267 /* and truncate port */
268 range->max.port = range->min.port + n_ports - 1;
274 * gst_rtsp_address_pool_acquire_address:
275 * @pool: a #GstRTSPAddressPool
277 * @n_ports: the amount of ports
278 * @address: result address
282 * Take an address and ports from @pool. @flags can be used to control the
283 * allocation. @n_ports consecutive ports will be allocated of which the first
284 * one can be found in @port.
286 * Returns: a pointer that should be used to release the address with
287 * gst_rtsp_address_pool_release_address() after usage or %NULL when no
288 * address could be acquired.
291 gst_rtsp_address_pool_acquire_address (GstRTSPAddressPool * pool,
292 GstRTSPAddressFlags flags, gint n_ports, gchar ** address,
293 guint16 * port, guint8 * ttl)
295 GstRTSPAddressPoolPrivate *priv;
299 g_return_val_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool), NULL);
300 g_return_val_if_fail (n_ports > 0, NULL);
301 g_return_val_if_fail (address != NULL, NULL);
302 g_return_val_if_fail (port != NULL, NULL);
303 g_return_val_if_fail (ttl != NULL, NULL);
308 g_mutex_lock (&priv->lock);
309 /* go over available ranges */
310 for (walk = priv->addresses; walk; walk = next) {
317 /* check address type when given */
318 if (flags & GST_RTSP_ADDRESS_FLAG_IPV4 && !ADDR_IS_IPV4 (&range->min))
320 if (flags & GST_RTSP_ADDRESS_FLAG_IPV6 && !ADDR_IS_IPV6 (&range->min))
323 /* check for enough ports */
324 ports = range->max.port - range->min.port + 1;
325 if (flags & GST_RTSP_ADDRESS_FLAG_EVEN_PORT
326 && !ADDR_IS_EVEN_PORT (&range->min))
330 if (ports - skip < n_ports)
333 /* we found a range, remove from the list */
334 priv->addresses = g_list_delete_link (priv->addresses, walk);
335 /* now split and exit our loop */
336 result = split_range (pool, range, skip, n_ports);
337 priv->allocated = g_list_prepend (priv->allocated, result);
340 g_mutex_unlock (&priv->lock);
343 *address = get_address_string (&result->min);
344 *port = result->min.port;
347 GST_DEBUG_OBJECT (pool, "got address %s:%u ttl %u", *address, *port, *ttl);
353 * gst_rtsp_address_pool_release_address:
354 * @pool: a #GstRTSPAddressPool
357 * Release a previously acquired address (with
358 * gst_rtsp_address_pool_acquire_address()) back into @pool.
361 gst_rtsp_address_pool_release_address (GstRTSPAddressPool * pool, gpointer id)
363 GstRTSPAddressPoolPrivate *priv;
366 g_return_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool));
367 g_return_if_fail (id != NULL);
371 g_mutex_lock (&priv->lock);
372 find = g_list_find (priv->allocated, id);
376 priv->allocated = g_list_delete_link (priv->allocated, find);
378 /* FIXME, merge and do something clever */
379 priv->addresses = g_list_prepend (priv->addresses, id);
380 g_mutex_unlock (&priv->lock);
387 g_warning ("Released unknown id %p", id);
388 g_mutex_unlock (&priv->lock);
394 dump_range (AddrRange * range, GstRTSPAddressPool * pool)
396 gchar *addr1, *addr2;
398 addr1 = get_address_string (&range->min);
399 addr2 = get_address_string (&range->max);
400 g_print (" address %s-%s, port %u-%u, ttl %u\n", addr1, addr2,
401 range->min.port, range->max.port, range->ttl);
407 * gst_rtsp_address_pool_dump:
408 * @pool: a #GstRTSPAddressPool
410 * Dump the free and allocated addresses to stdout.
413 gst_rtsp_address_pool_dump (GstRTSPAddressPool * pool)
415 GstRTSPAddressPoolPrivate *priv;
417 g_return_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool));
421 g_mutex_lock (&priv->lock);
423 g_list_foreach (priv->addresses, (GFunc) dump_range, pool);
424 g_print ("allocated:\n");
425 g_list_foreach (priv->allocated, (GFunc) dump_range, pool);
426 g_mutex_unlock (&priv->lock);