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"
26 gst_rtsp_address_copy (GstRTSPAddress * addr)
30 g_return_val_if_fail (addr != NULL, NULL);
32 copy = g_slice_dup (GstRTSPAddress, addr);
33 /* only release to the pool when the original is freed. It's a bit
34 * weird but this will do for now as it avoid us to use refcounting. */
36 copy->address = g_strdup (copy->address);
41 static void gst_rtsp_address_pool_release_address (GstRTSPAddressPool * pool,
42 GstRTSPAddress * addr);
45 gst_rtsp_address_free (GstRTSPAddress * addr)
47 g_return_if_fail (addr != NULL);
50 /* unrefs the pool and sets it to NULL */
51 gst_rtsp_address_pool_release_address (addr->pool, addr);
53 g_free (addr->address);
54 g_slice_free (GstRTSPAddress, addr);
58 G_DEFINE_BOXED_TYPE (GstRTSPAddress, gst_rtsp_address,
59 (GBoxedCopyFunc) gst_rtsp_address_copy,
60 (GBoxedFreeFunc) gst_rtsp_address_free);
62 GST_DEBUG_CATEGORY_STATIC (rtsp_address_pool_debug);
63 #define GST_CAT_DEFAULT rtsp_address_pool_debug
65 #define GST_RTSP_ADDRESS_POOL_GET_PRIVATE(obj) \
66 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_ADDRESS_POOL, GstRTSPAddressPoolPrivate))
68 struct _GstRTSPAddressPoolPrivate
75 #define ADDR_IS_IPV4(a) ((a)->size == 4)
76 #define ADDR_IS_IPV6(a) ((a)->size == 16)
77 #define ADDR_IS_EVEN_PORT(a) (((a)->port & 1) == 0)
93 #define RANGE_IS_SINGLE(r) (memcmp ((r)->min.bytes, (r)->max.bytes, (r)->min.size) == 0)
95 #define gst_rtsp_address_pool_parent_class parent_class
96 G_DEFINE_TYPE (GstRTSPAddressPool, gst_rtsp_address_pool, G_TYPE_OBJECT);
98 static void gst_rtsp_address_pool_finalize (GObject * obj);
101 gst_rtsp_address_pool_class_init (GstRTSPAddressPoolClass * klass)
103 GObjectClass *gobject_class;
105 gobject_class = G_OBJECT_CLASS (klass);
107 gobject_class->finalize = gst_rtsp_address_pool_finalize;
109 g_type_class_add_private (klass, sizeof (GstRTSPAddressPoolPrivate));
111 GST_DEBUG_CATEGORY_INIT (rtsp_address_pool_debug, "rtspaddresspool", 0,
112 "GstRTSPAddressPool");
116 gst_rtsp_address_pool_init (GstRTSPAddressPool * pool)
118 pool->priv = GST_RTSP_ADDRESS_POOL_GET_PRIVATE (pool);
120 g_mutex_init (&pool->priv->lock);
124 free_range (AddrRange * range)
126 g_slice_free (AddrRange, range);
130 gst_rtsp_address_pool_finalize (GObject * obj)
132 GstRTSPAddressPool *pool;
134 pool = GST_RTSP_ADDRESS_POOL (obj);
136 g_list_free_full (pool->priv->addresses, (GDestroyNotify) free_range);
137 g_list_free_full (pool->priv->allocated, (GDestroyNotify) free_range);
138 g_mutex_clear (&pool->priv->lock);
140 G_OBJECT_CLASS (parent_class)->finalize (obj);
144 * gst_rtsp_address_pool_new:
146 * Make a new #GstRTSPAddressPool.
148 * Returns: a new #GstRTSPAddressPool
151 gst_rtsp_address_pool_new (void)
153 GstRTSPAddressPool *pool;
155 pool = g_object_new (GST_TYPE_RTSP_ADDRESS_POOL, NULL);
161 * gst_rtsp_address_pool_clear:
162 * @pool: a #GstRTSPAddressPool
164 * Clear all addresses in @pool. There should be no outstanding
168 gst_rtsp_address_pool_clear (GstRTSPAddressPool * pool)
170 GstRTSPAddressPoolPrivate *priv;
172 g_return_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool));
173 g_return_if_fail (pool->priv->allocated == NULL);
177 g_mutex_lock (&priv->lock);
178 g_list_free_full (priv->addresses, (GDestroyNotify) free_range);
179 priv->addresses = NULL;
180 g_mutex_unlock (&priv->lock);
184 fill_address (const gchar * address, guint16 port, Addr * addr)
188 inet = g_inet_address_new_from_string (address);
192 addr->size = g_inet_address_get_native_size (inet);
193 memcpy (addr->bytes, g_inet_address_to_bytes (inet), addr->size);
194 g_object_unref (inet);
201 get_address_string (Addr * addr)
206 inet = g_inet_address_new_from_bytes (addr->bytes,
207 addr->size == 4 ? G_SOCKET_FAMILY_IPV4 : G_SOCKET_FAMILY_IPV6);
208 res = g_inet_address_to_string (inet);
209 g_object_unref (inet);
215 * gst_rtsp_address_pool_add_range:
216 * @pool: a #GstRTSPAddressPool
217 * @min_address: a minimum address to add
218 * @max_address: a maximum address to add
219 * @min_port: the minimum port
220 * @max_port: the maximum port
223 * Adds the multicast addresses from @min_addess to @max_address (inclusive)
224 * to @pool. The valid port range for the addresses will be from @min_port to
225 * @max_port inclusive.
227 * Returns: %TRUE if the addresses could be added.
230 gst_rtsp_address_pool_add_range (GstRTSPAddressPool * pool,
231 const gchar * min_address, const gchar * max_address,
232 guint16 min_port, guint16 max_port, guint8 ttl)
235 GstRTSPAddressPoolPrivate *priv;
237 g_return_val_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool), FALSE);
238 g_return_val_if_fail (min_port <= max_port, FALSE);
242 range = g_slice_new0 (AddrRange);
244 if (!fill_address (min_address, min_port, &range->min))
246 if (!fill_address (max_address, max_port, &range->max))
249 if (range->min.size != range->max.size)
251 if (memcmp (range->min.bytes, range->max.bytes, range->min.size) > 0)
256 GST_DEBUG_OBJECT (pool, "adding %s-%s:%u-%u ttl %u", min_address, max_address,
257 min_port, max_port, ttl);
259 g_mutex_lock (&priv->lock);
260 priv->addresses = g_list_prepend (priv->addresses, range);
261 g_mutex_unlock (&priv->lock);
268 GST_ERROR_OBJECT (pool, "invalid address range %s-%s", min_address,
270 g_slice_free (AddrRange, range);
275 /* increments the address by count */
278 inc_address (Addr * addr, guint8 count)
284 for (i = addr->size - 1; i >= 0 && carry > 0; i--) {
285 carry += addr->bytes[i];
286 addr->bytes[i] = carry & 0xff;
291 /* tells us the number of addresses between min_addr and max_addr */
294 diff_address (Addr * max_addr, Addr * min_addr)
299 g_return_val_if_fail (min_addr->size == max_addr->size, 0);
301 for (i = 0; i < min_addr->size; i++) {
302 g_return_val_if_fail (result < (1 << 24), result);
305 result += max_addr->bytes[i] - min_addr->bytes[i];
313 split_range (GstRTSPAddressPool * pool, AddrRange * range, guint skip_addr,
314 guint skip_port, gint n_ports)
316 GstRTSPAddressPoolPrivate *priv = pool->priv;
320 temp = g_slice_dup (AddrRange, range);
321 memcpy (temp->max.bytes, temp->min.bytes, temp->min.size);
322 inc_address (&temp->max, skip_addr - 1);
323 priv->addresses = g_list_prepend (priv->addresses, temp);
325 inc_address (&range->min, skip_addr);
328 if (!RANGE_IS_SINGLE (range)) {
329 /* min and max are not the same, we have more than one address. */
330 temp = g_slice_dup (AddrRange, range);
331 /* increment the range min address */
332 inc_address (&temp->min, 1);
333 /* and store back in pool */
334 priv->addresses = g_list_prepend (priv->addresses, temp);
336 /* adjust range with only the first address */
337 memcpy (range->max.bytes, range->min.bytes, range->min.size);
340 /* range now contains only one single address */
342 /* make a range with the skipped ports */
343 temp = g_slice_dup (AddrRange, range);
344 temp->max.port = temp->min.port + skip_port - 1;
345 /* and store back in pool */
346 priv->addresses = g_list_prepend (priv->addresses, temp);
348 /* increment range port */
349 range->min.port += skip_port;
351 /* range now contains single address with desired port number */
352 if (range->max.port - range->min.port + 1 > n_ports) {
353 /* make a range with the remaining ports */
354 temp = g_slice_dup (AddrRange, range);
355 temp->min.port += n_ports;
356 /* and store back in pool */
357 priv->addresses = g_list_prepend (priv->addresses, temp);
359 /* and truncate port */
360 range->max.port = range->min.port + n_ports - 1;
366 * gst_rtsp_address_pool_acquire_address:
367 * @pool: a #GstRTSPAddressPool
369 * @n_ports: the amount of ports
371 * Take an address and ports from @pool. @flags can be used to control the
372 * allocation. @n_ports consecutive ports will be allocated of which the first
373 * one can be found in @port.
375 * Returns: a #GstRTSPAddress that should be freed with gst_rtsp_address_free
376 * after use or %NULL when no address could be acquired.
379 gst_rtsp_address_pool_acquire_address (GstRTSPAddressPool * pool,
380 GstRTSPAddressFlags flags, gint n_ports)
382 GstRTSPAddressPoolPrivate *priv;
385 GstRTSPAddress *addr;
387 g_return_val_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool), NULL);
388 g_return_val_if_fail (n_ports > 0, NULL);
394 g_mutex_lock (&priv->lock);
395 /* go over available ranges */
396 for (walk = priv->addresses; walk; walk = next) {
403 /* check address type when given */
404 if (flags & GST_RTSP_ADDRESS_FLAG_IPV4 && !ADDR_IS_IPV4 (&range->min))
406 if (flags & GST_RTSP_ADDRESS_FLAG_IPV6 && !ADDR_IS_IPV6 (&range->min))
409 /* check for enough ports */
410 ports = range->max.port - range->min.port + 1;
411 if (flags & GST_RTSP_ADDRESS_FLAG_EVEN_PORT
412 && !ADDR_IS_EVEN_PORT (&range->min))
416 if (ports - skip < n_ports)
419 /* we found a range, remove from the list */
420 priv->addresses = g_list_delete_link (priv->addresses, walk);
421 /* now split and exit our loop */
422 result = split_range (pool, range, 0, skip, n_ports);
423 priv->allocated = g_list_prepend (priv->allocated, result);
426 g_mutex_unlock (&priv->lock);
429 addr = g_slice_new0 (GstRTSPAddress);
430 addr->pool = g_object_ref (pool);
431 addr->address = get_address_string (&result->min);
432 addr->n_ports = n_ports;
433 addr->port = result->min.port;
434 addr->ttl = result->ttl;
437 GST_DEBUG_OBJECT (pool, "got address %s:%u ttl %u", addr->address,
438 addr->port, addr->ttl);
445 * gst_rtsp_address_pool_release_address:
446 * @pool: a #GstRTSPAddressPool
449 * Release a previously acquired address (with
450 * gst_rtsp_address_pool_acquire_address()) back into @pool.
453 gst_rtsp_address_pool_release_address (GstRTSPAddressPool * pool,
454 GstRTSPAddress * addr)
456 GstRTSPAddressPoolPrivate *priv;
460 g_return_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool));
461 g_return_if_fail (addr != NULL);
462 g_return_if_fail (addr->pool == pool);
467 /* we don't want to free twice */
471 g_mutex_lock (&priv->lock);
472 find = g_list_find (priv->allocated, range);
476 priv->allocated = g_list_delete_link (priv->allocated, find);
478 /* FIXME, merge and do something clever */
479 priv->addresses = g_list_prepend (priv->addresses, range);
480 g_mutex_unlock (&priv->lock);
482 g_object_unref (pool);
489 g_warning ("Released unknown address %p", addr);
490 g_mutex_unlock (&priv->lock);
496 dump_range (AddrRange * range, GstRTSPAddressPool * pool)
498 gchar *addr1, *addr2;
500 addr1 = get_address_string (&range->min);
501 addr2 = get_address_string (&range->max);
502 g_print (" address %s-%s, port %u-%u, ttl %u\n", addr1, addr2,
503 range->min.port, range->max.port, range->ttl);
509 * gst_rtsp_address_pool_dump:
510 * @pool: a #GstRTSPAddressPool
512 * Dump the free and allocated addresses to stdout.
515 gst_rtsp_address_pool_dump (GstRTSPAddressPool * pool)
517 GstRTSPAddressPoolPrivate *priv;
519 g_return_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool));
523 g_mutex_lock (&priv->lock);
525 g_list_foreach (priv->addresses, (GFunc) dump_range, pool);
526 g_print ("allocated:\n");
527 g_list_foreach (priv->allocated, (GFunc) dump_range, pool);
528 g_mutex_unlock (&priv->lock);
532 * gst_rtsp_address_pool_reserve_address:
533 * @pool: a #GstRTSPAddressPool
534 * @address: The IP address to reserve
535 * @port: The first port to reserve
536 * @n_ports: The number of ports
537 * @ttl: The requested ttl
539 * Take a specific address and ports from @pool. @n_ports consecutive
540 * ports will be allocated of which the first one can be found in
543 * Returns: a #GstRTSPAddress that should be freed with gst_rtsp_address_free
544 * after use or %NULL when no address could be acquired.
548 gst_rtsp_address_pool_reserve_address (GstRTSPAddressPool * pool,
549 const gchar * address, guint port, guint n_ports, guint ttl)
551 GstRTSPAddressPoolPrivate *priv;
555 GstRTSPAddress *addr;
557 g_return_val_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool), NULL);
558 g_return_val_if_fail (address != NULL, NULL);
559 g_return_val_if_fail (port > 0, NULL);
560 g_return_val_if_fail (n_ports > 0, NULL);
566 if (!fill_address (address, port, &input_addr)) {
567 GST_ERROR_OBJECT (pool, "invalid address %s", address);
571 g_mutex_lock (&priv->lock);
572 /* go over available ranges */
573 for (walk = priv->addresses; walk; walk = next) {
575 gint skip_port, skip_addr;
580 /* Not the right type of address */
581 if (range->min.size != input_addr.size)
584 /* Check that the address is in the interval */
585 if (memcmp (range->min.bytes, input_addr.bytes, input_addr.size) > 0 ||
586 memcmp (range->max.bytes, input_addr.bytes, input_addr.size) < 0)
589 /* Make sure the requested ports are inside the range */
590 if (port < range->min.port || port + n_ports - 1 > range->max.port)
593 if (ttl != range->ttl)
596 skip_addr = diff_address (&input_addr, &range->min);
597 skip_port = port - range->min.port;
599 /* we found a range, remove from the list */
600 priv->addresses = g_list_delete_link (priv->addresses, walk);
601 /* now split and exit our loop */
602 result = split_range (pool, range, skip_addr, skip_port, n_ports);
603 priv->allocated = g_list_prepend (priv->allocated, result);
606 g_mutex_unlock (&priv->lock);
609 addr = g_slice_new0 (GstRTSPAddress);
610 addr->pool = g_object_ref (pool);
611 addr->address = get_address_string (&result->min);
612 addr->n_ports = n_ports;
613 addr->port = result->min.port;
614 addr->ttl = result->ttl;
617 GST_DEBUG_OBJECT (pool, "reserved address %s:%u ttl %u", addr->address,
618 addr->port, addr->ttl);