address-pool: small cleanups
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-address-pool.c
1 /* GStreamer
2  * Copyright (C) 2012 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <string.h>
21 #include <gio/gio.h>
22
23 #include "rtsp-address-pool.h"
24
25 GST_DEBUG_CATEGORY_STATIC (rtsp_address_pool_debug);
26 #define GST_CAT_DEFAULT rtsp_address_pool_debug
27
28 #define GST_RTSP_ADDRESS_POOL_GET_PRIVATE(obj)  \
29      (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_ADDRESS_POOL, GstRTSPAddressPoolPrivate))
30
31 struct _GstRTSPAddressPoolPrivate
32 {
33   GMutex lock;
34   GList *addresses;
35   GList *allocated;
36 };
37
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)
41
42 typedef struct
43 {
44   guint8 bytes[16];
45   gsize size;
46   guint16 port;
47 } Addr;
48
49 typedef struct
50 {
51   Addr min;
52   Addr max;
53   guint8 ttl;
54 } AddrRange;
55
56 #define RANGE_IS_SINGLE(r) (memcmp ((r)->min.bytes, (r)->max.bytes, (r)->min.size) == 0)
57
58 #define gst_rtsp_address_pool_parent_class parent_class
59 G_DEFINE_TYPE (GstRTSPAddressPool, gst_rtsp_address_pool, G_TYPE_OBJECT);
60
61 static void gst_rtsp_address_pool_finalize (GObject * obj);
62
63 static void
64 gst_rtsp_address_pool_class_init (GstRTSPAddressPoolClass * klass)
65 {
66   GObjectClass *gobject_class;
67
68   gobject_class = G_OBJECT_CLASS (klass);
69
70   gobject_class->finalize = gst_rtsp_address_pool_finalize;
71
72   g_type_class_add_private (klass, sizeof (GstRTSPAddressPoolPrivate));
73
74   GST_DEBUG_CATEGORY_INIT (rtsp_address_pool_debug, "rtspaddresspool", 0,
75       "GstRTSPAddressPool");
76 }
77
78 static void
79 gst_rtsp_address_pool_init (GstRTSPAddressPool * pool)
80 {
81   pool->priv = GST_RTSP_ADDRESS_POOL_GET_PRIVATE (pool);
82
83   g_mutex_init (&pool->priv->lock);
84 }
85
86 static void
87 free_range (AddrRange * range)
88 {
89   g_slice_free (AddrRange, range);
90 }
91
92 static void
93 gst_rtsp_address_pool_finalize (GObject * obj)
94 {
95   GstRTSPAddressPool *pool;
96
97   pool = GST_RTSP_ADDRESS_POOL (obj);
98
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);
102
103   G_OBJECT_CLASS (parent_class)->finalize (obj);
104 }
105
106 /**
107  * gst_rtsp_address_pool_new:
108  *
109  * Make a new #GstRTSPAddressPool.
110  *
111  * Returns: a new #GstRTSPAddressPool
112  */
113 GstRTSPAddressPool *
114 gst_rtsp_address_pool_new (void)
115 {
116   GstRTSPAddressPool *pool;
117
118   pool = g_object_new (GST_TYPE_RTSP_ADDRESS_POOL, NULL);
119
120   return pool;
121 }
122
123 static gboolean
124 fill_address (const gchar * address, guint16 port, Addr * addr)
125 {
126   GInetAddress *inet;
127
128   inet = g_inet_address_new_from_string (address);
129   if (inet == NULL)
130     return FALSE;
131
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);
135   addr->port = port;
136
137   return TRUE;
138 }
139
140 static gchar *
141 get_address_string (Addr * addr)
142 {
143   gchar *res;
144   GInetAddress *inet;
145
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);
150
151   return res;
152 }
153
154 /**
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
161  * @ttl: a TTL
162  *
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.
166  *
167  * Returns: %TRUE if the addresses could be added.
168  */
169 gboolean
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)
173 {
174   AddrRange *range;
175   GstRTSPAddressPoolPrivate *priv;
176
177   g_return_val_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool), FALSE);
178   g_return_val_if_fail (min_port <= max_port, FALSE);
179
180   priv = pool->priv;
181
182   range = g_slice_new0 (AddrRange);
183
184   if (!fill_address (min_address, min_port, &range->min))
185     goto invalid;
186   if (!fill_address (max_address, max_port, &range->max))
187     goto invalid;
188
189   if (range->min.size != range->max.size)
190     goto invalid;
191   if (memcmp (range->min.bytes, range->max.bytes, range->min.size) > 0)
192     goto invalid;
193
194   range->ttl = ttl;
195
196   GST_DEBUG_OBJECT (pool, "adding %s-%s:%u-%u ttl %u", min_address, max_address,
197       min_port, max_port, ttl);
198
199   g_mutex_lock (&priv->lock);
200   priv->addresses = g_list_prepend (priv->addresses, range);
201   g_mutex_unlock (&priv->lock);
202
203   return TRUE;
204
205   /* ERRORS */
206 invalid:
207   {
208     GST_ERROR_OBJECT (pool, "invalid address range %s-%s", min_address,
209         max_address);
210     g_slice_free (AddrRange, range);
211     return FALSE;
212   }
213 }
214
215 static void
216 inc_address (GstRTSPAddressPool * pool, Addr * addr)
217 {
218   gint i;
219   guint carry;
220
221   carry = 1;
222   for (i = addr->size - 1; i >= 0 && carry > 0; i--) {
223     carry += addr->bytes[i];
224     addr->bytes[i] = carry & 0xff;
225     carry >>= 8;
226   }
227 }
228
229 static AddrRange *
230 split_range (GstRTSPAddressPool * pool, AddrRange * range, gint skip,
231     gint n_ports)
232 {
233   GstRTSPAddressPoolPrivate *priv = pool->priv;
234   AddrRange *temp;
235
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);
243
244     /* adjust range with only the first address */
245     memcpy (range->max.bytes, range->min.bytes, range->min.size);
246   }
247
248   /* range now contains only one single address */
249   if (skip > 0) {
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);
255
256     /* increment range port */
257     range->min.port += skip;
258   }
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);
266
267     /* and truncate port */
268     range->max.port = range->min.port + n_ports - 1;
269   }
270   return range;
271 }
272
273 /**
274  * gst_rtsp_address_pool_acquire_address:
275  * @pool: a #GstRTSPAddressPool
276  * @flags: flags
277  * @n_ports: the amount of ports
278  * @address: result address
279  * @port: result port
280  * @ttl: result TTL
281  *
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.
285  *
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.
289  */
290 gpointer
291 gst_rtsp_address_pool_acquire_address (GstRTSPAddressPool * pool,
292     GstRTSPAddressFlags flags, gint n_ports, gchar ** address,
293     guint16 * port, guint8 * ttl)
294 {
295   GstRTSPAddressPoolPrivate *priv;
296   GList *walk, *next;
297   AddrRange *result;
298
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);
304
305   priv = pool->priv;
306   result = NULL;
307
308   g_mutex_lock (&priv->lock);
309   /* go over available ranges */
310   for (walk = priv->addresses; walk; walk = next) {
311     AddrRange *range;
312     gint ports, skip;
313
314     range = walk->data;
315     next = walk->next;
316
317     /* check address type when given */
318     if (flags & GST_RTSP_ADDRESS_FLAG_IPV4 && !ADDR_IS_IPV4 (&range->min))
319       continue;
320     if (flags & GST_RTSP_ADDRESS_FLAG_IPV6 && !ADDR_IS_IPV6 (&range->min))
321       continue;
322
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))
327       skip = 1;
328     else
329       skip = 0;
330     if (ports - skip < n_ports)
331       continue;
332
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);
338     break;
339   }
340   g_mutex_unlock (&priv->lock);
341
342   if (result) {
343     *address = get_address_string (&result->min);
344     *port = result->min.port;
345     *ttl = result->ttl;
346
347     GST_DEBUG_OBJECT (pool, "got address %s:%u ttl %u", *address, *port, *ttl);
348   }
349   return result;
350 }
351
352 /**
353  * gst_rtsp_address_pool_release_address:
354  * @pool: a #GstRTSPAddressPool
355  * @id: an address id
356  *
357  * Release a previously acquired address (with
358  * gst_rtsp_address_pool_acquire_address()) back into @pool.
359  */
360 void
361 gst_rtsp_address_pool_release_address (GstRTSPAddressPool * pool, gpointer id)
362 {
363   GstRTSPAddressPoolPrivate *priv;
364   GList *find;
365
366   g_return_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool));
367   g_return_if_fail (id != NULL);
368
369   priv = pool->priv;
370
371   g_mutex_lock (&priv->lock);
372   find = g_list_find (priv->allocated, id);
373   if (find == NULL)
374     goto not_found;
375
376   priv->allocated = g_list_delete_link (priv->allocated, find);
377
378   /* FIXME, merge and do something clever */
379   priv->addresses = g_list_prepend (priv->addresses, id);
380   g_mutex_unlock (&priv->lock);
381
382   return;
383
384   /* ERRORS */
385 not_found:
386   {
387     g_warning ("Released unknown id %p", id);
388     g_mutex_unlock (&priv->lock);
389     return;
390   }
391 }
392
393 static void
394 dump_range (AddrRange * range, GstRTSPAddressPool * pool)
395 {
396   gchar *addr1, *addr2;
397
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);
402   g_free (addr1);
403   g_free (addr2);
404 }
405
406 /**
407  * gst_rtsp_address_pool_dump:
408  * @pool: a #GstRTSPAddressPool
409  *
410  * Dump the free and allocated addresses to stdout.
411  */
412 void
413 gst_rtsp_address_pool_dump (GstRTSPAddressPool * pool)
414 {
415   GstRTSPAddressPoolPrivate *priv;
416
417   g_return_if_fail (GST_IS_RTSP_ADDRESS_POOL (pool));
418
419   priv = pool->priv;
420
421   g_mutex_lock (&priv->lock);
422   g_print ("free:\n");
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);
427 }