gtestdbus: Fix non-const use of const variables
[platform/upstream/glib.git] / gio / ginetaddressmask.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright 2011 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "ginetaddressmask.h"
27 #include "ginetaddress.h"
28 #include "ginitable.h"
29 #include "gioerror.h"
30 #include "gioenumtypes.h"
31 #include "glibintl.h"
32
33 /**
34  * SECTION:ginetaddressmask
35  * @short_description: An IPv4/IPv6 address mask
36  *
37  * #GInetAddressMask represents a range of IPv4 or IPv6 addresses
38  * described by a base address and a length indicating how many bits
39  * of the base address are relevant for matching purposes. These are
40  * often given in string form. Eg, "10.0.0.0/8", or "fe80::/10".
41  */
42
43 /**
44  * GInetAddressMask:
45  *
46  * A combination of an IPv4 or IPv6 base address and a length,
47  * representing a range of IP addresses.
48  *
49  * Since: 2.32
50  */
51
52 struct _GInetAddressMaskPrivate
53 {
54   GInetAddress *addr;
55   guint         length;
56 };
57
58 static void     g_inet_address_mask_initable_iface_init (GInitableIface  *iface);
59
60 G_DEFINE_TYPE_WITH_CODE (GInetAddressMask, g_inet_address_mask, G_TYPE_OBJECT,
61                          G_ADD_PRIVATE (GInetAddressMask)
62                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
63                                                 g_inet_address_mask_initable_iface_init))
64
65 enum
66 {
67   PROP_0,
68   PROP_FAMILY,
69   PROP_ADDRESS,
70   PROP_LENGTH
71 };
72
73 static void
74 g_inet_address_mask_set_property (GObject      *object,
75                                   guint         prop_id,
76                                   const GValue *value,
77                                   GParamSpec   *pspec)
78 {
79   GInetAddressMask *mask = G_INET_ADDRESS_MASK (object);
80
81   switch (prop_id)
82     {
83     case PROP_ADDRESS:
84       if (mask->priv->addr)
85         g_object_unref (mask->priv->addr);
86       mask->priv->addr = g_value_dup_object (value);
87       break;
88
89     case PROP_LENGTH:
90       mask->priv->length = g_value_get_uint (value);
91       break;
92
93     default:
94       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95       break;
96     }
97
98 }
99
100 static void
101 g_inet_address_mask_get_property (GObject    *object,
102                                   guint       prop_id,
103                                   GValue     *value,
104                                   GParamSpec *pspec)
105 {
106   GInetAddressMask *mask = G_INET_ADDRESS_MASK (object);
107
108   switch (prop_id)
109     {
110     case PROP_FAMILY:
111       g_value_set_enum (value, g_inet_address_get_family (mask->priv->addr));
112       break;
113
114     case PROP_ADDRESS:
115       g_value_set_object (value, mask->priv->addr);
116       break;
117
118     case PROP_LENGTH:
119       g_value_set_uint (value, mask->priv->length);
120       break;
121
122     default:
123       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124     }
125 }
126
127 static void
128 g_inet_address_mask_dispose (GObject *object)
129 {
130   GInetAddressMask *mask = G_INET_ADDRESS_MASK (object);
131
132   g_clear_object (&mask->priv->addr);
133
134   G_OBJECT_CLASS (g_inet_address_mask_parent_class)->dispose (object);
135 }
136
137 static void
138 g_inet_address_mask_class_init (GInetAddressMaskClass *klass)
139 {
140   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
141
142   gobject_class->set_property = g_inet_address_mask_set_property;
143   gobject_class->get_property = g_inet_address_mask_get_property;
144   gobject_class->dispose = g_inet_address_mask_dispose;
145
146   g_object_class_install_property (gobject_class, PROP_FAMILY,
147                                    g_param_spec_enum ("family",
148                                                       P_("Address family"),
149                                                       P_("The address family (IPv4 or IPv6)"),
150                                                       G_TYPE_SOCKET_FAMILY,
151                                                       G_SOCKET_FAMILY_INVALID,
152                                                       G_PARAM_READABLE |
153                                                       G_PARAM_STATIC_STRINGS));
154   g_object_class_install_property (gobject_class, PROP_ADDRESS,
155                                    g_param_spec_object ("address",
156                                                         P_("Address"),
157                                                         P_("The base address"),
158                                                         G_TYPE_INET_ADDRESS,
159                                                         G_PARAM_READWRITE |
160                                                         G_PARAM_STATIC_STRINGS));
161   g_object_class_install_property (gobject_class, PROP_LENGTH,
162                                    g_param_spec_uint ("length",
163                                                       P_("Length"),
164                                                       P_("The prefix length"),
165                                                       0, 128, 0,
166                                                       G_PARAM_READWRITE |
167                                                       G_PARAM_STATIC_STRINGS));
168 }
169
170 static gboolean
171 g_inet_address_mask_initable_init (GInitable     *initable,
172                                    GCancellable  *cancellable,
173                                    GError       **error)
174 {
175   GInetAddressMask *mask = G_INET_ADDRESS_MASK (initable);
176   guint addrlen, nbytes, nbits;
177   const guint8 *bytes;
178   gboolean ok;
179
180   if (!mask->priv->addr)
181     {
182       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
183                            _("No address specified"));
184       return FALSE;
185     }
186
187   addrlen = g_inet_address_get_native_size (mask->priv->addr);
188   if (mask->priv->length > addrlen * 8)
189     {
190       g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
191                    _("Length %u is too long for address"),
192                    mask->priv->length);
193       return FALSE;
194     }
195
196   /* Make sure all the bits after @length are 0 */
197   bytes = g_inet_address_to_bytes (mask->priv->addr);
198   ok = TRUE;
199
200   nbytes = mask->priv->length / 8;
201   bytes += nbytes;
202   addrlen -= nbytes;
203
204   nbits = mask->priv->length % 8;
205   if (nbits)
206     {
207       if (bytes[0] & (0xFF >> nbits))
208         ok = FALSE;
209       bytes++;
210       addrlen--;
211     }
212
213   while (addrlen)
214     {
215       if (bytes[0])
216         ok = FALSE;
217       bytes++;
218       addrlen--;
219     }
220
221   if (!ok)
222     {
223       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
224                            _("Address has bits set beyond prefix length"));
225       return FALSE;
226     }
227
228   return TRUE;
229 }
230
231 static void
232 g_inet_address_mask_initable_iface_init (GInitableIface  *iface)
233 {
234   iface->init = g_inet_address_mask_initable_init;
235 }
236
237 static void
238 g_inet_address_mask_init (GInetAddressMask *mask)
239 {
240   mask->priv = g_inet_address_mask_get_instance_private (mask);
241 }
242
243 /**
244  * g_inet_address_mask_new:
245  * @addr: a #GInetAddress
246  * @length: number of bits of @addr to use
247  * @error: return location for #GError, or %NULL
248  *
249  * Creates a new #GInetAddressMask representing all addresses whose
250  * first @length bits match @addr.
251  *
252  * Returns: a new #GInetAddressMask, or %NULL on error
253  *
254  * Since: 2.32
255  */
256 GInetAddressMask *
257 g_inet_address_mask_new (GInetAddress  *addr,
258                          guint          length,
259                          GError       **error)
260 {
261   return g_initable_new (G_TYPE_INET_ADDRESS_MASK, NULL, error,
262                          "address", addr,
263                          "length", length,
264                          NULL);
265 }
266
267 /**
268  * g_inet_address_mask_new_from_string:
269  * @mask_string: an IP address or address/length string
270  * @error: return location for #GError, or %NULL
271  *
272  * Parses @mask_string as an IP address and (optional) length, and
273  * creates a new #GInetAddressMask. The length, if present, is
274  * delimited by a "/". If it is not present, then the length is
275  * assumed to be the full length of the address.
276  *
277  * Returns: a new #GInetAddressMask corresponding to @string, or %NULL
278  * on error.
279  *
280  * Since: 2.32
281  */
282 GInetAddressMask *
283 g_inet_address_mask_new_from_string (const gchar  *mask_string,
284                                      GError      **error)
285 {
286   GInetAddressMask *mask;
287   GInetAddress *addr;
288   gchar *slash;
289   guint length;
290
291   slash = strchr (mask_string, '/');
292   if (slash)
293     {
294       gchar *address, *end;
295
296       length = strtoul (slash + 1, &end, 10);
297       if (*end || !*(slash + 1))
298         {
299         parse_error:
300           g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
301                        _("Could not parse '%s' as IP address mask"),
302                        mask_string);
303           return NULL;
304         }
305
306       address = g_strndup (mask_string, slash - mask_string);
307       addr = g_inet_address_new_from_string (address);
308       g_free (address);
309
310       if (!addr)
311         goto parse_error;
312     }
313   else
314     {
315       addr = g_inet_address_new_from_string (mask_string);
316       if (!addr)
317         goto parse_error;
318
319       length = g_inet_address_get_native_size (addr) * 8;
320     }
321
322   mask = g_inet_address_mask_new (addr, length, error);
323   g_object_unref (addr);
324
325   return mask;
326 }
327
328 /**
329  * g_inet_address_mask_to_string:
330  * @mask: a #GInetAddressMask
331  *
332  * Converts @mask back to its corresponding string form.
333  *
334  * Return value: a string corresponding to @mask.
335  *
336  * Since: 2.32
337  */
338 gchar *
339 g_inet_address_mask_to_string (GInetAddressMask *mask)
340 {
341   gchar *addr_string, *mask_string;
342
343   g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), NULL);
344
345   addr_string = g_inet_address_to_string (mask->priv->addr);
346
347   if (mask->priv->length == (g_inet_address_get_native_size (mask->priv->addr) * 8))
348     return addr_string;
349
350   mask_string = g_strdup_printf ("%s/%u", addr_string, mask->priv->length);
351   g_free (addr_string);
352
353   return mask_string;
354 }
355
356 /**
357  * g_inet_address_mask_get_family:
358  * @mask: a #GInetAddressMask
359  *
360  * Gets the #GSocketFamily of @mask's address
361  *
362  * Return value: the #GSocketFamily of @mask's address
363  *
364  * Since: 2.32
365  */
366 GSocketFamily
367 g_inet_address_mask_get_family (GInetAddressMask *mask)
368 {
369   g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), G_SOCKET_FAMILY_INVALID);
370
371   return g_inet_address_get_family (mask->priv->addr);
372 }
373
374 /**
375  * g_inet_address_mask_get_address:
376  * @mask: a #GInetAddressMask
377  *
378  * Gets @mask's base address
379  *
380  * Return value: (transfer none): @mask's base address
381  *
382  * Since: 2.32
383  */
384 GInetAddress *
385 g_inet_address_mask_get_address (GInetAddressMask *mask)
386 {
387   g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), NULL);
388
389   return mask->priv->addr;
390 }
391
392 /**
393  * g_inet_address_mask_get_length:
394  * @mask: a #GInetAddressMask
395  *
396  * Gets @mask's length
397  *
398  * Return value: @mask's length
399  *
400  * Since: 2.32
401  */
402 guint
403 g_inet_address_mask_get_length (GInetAddressMask *mask)
404 {
405   g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), 0);
406
407   return mask->priv->length;
408 }
409
410 /**
411  * g_inet_address_mask_matches:
412  * @mask: a #GInetAddressMask
413  * @address: a #GInetAddress
414  *
415  * Tests if @address falls within the range described by @mask.
416  *
417  * Return value: whether @address falls within the range described by
418  * @mask.
419  *
420  * Since: 2.32
421  */
422 gboolean
423 g_inet_address_mask_matches (GInetAddressMask *mask,
424                              GInetAddress     *address)
425 {
426   const guint8 *maskbytes, *addrbytes;
427   int nbytes, nbits;
428
429   g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), FALSE);
430   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
431
432   if (g_inet_address_get_family (mask->priv->addr) !=
433       g_inet_address_get_family (address))
434     return FALSE;
435
436   if (mask->priv->length == 0)
437     return TRUE;
438
439   maskbytes = g_inet_address_to_bytes (mask->priv->addr);
440   addrbytes = g_inet_address_to_bytes (address);
441
442   nbytes = mask->priv->length / 8;
443   if (nbytes != 0 && memcmp (maskbytes, addrbytes, nbytes) != 0)
444     return FALSE;
445
446   nbits = mask->priv->length % 8;
447   if (nbits == 0)
448     return TRUE;
449
450   return maskbytes[nbytes] == (addrbytes[nbytes] & (0xFF << (8 - nbits)));
451 }
452
453
454 /**
455  * g_inet_address_mask_equal:
456  * @mask: a #GInetAddressMask
457  * @mask2: another #GInetAddressMask
458  *
459  * Tests if @mask and @mask2 are the same mask.
460  *
461  * Return value: whether @mask and @mask2 are the same mask
462  *
463  * Since: 2.32
464  */
465 gboolean
466 g_inet_address_mask_equal (GInetAddressMask  *mask,
467                            GInetAddressMask  *mask2)
468 {
469   return ((mask->priv->length == mask2->priv->length) &&
470           g_inet_address_equal (mask->priv->addr, mask2->priv->addr));
471 }