gio/tests/file: skip the file monitor tests if using GPollFileMonitor
[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 static void     g_inet_address_mask_initable_iface_init (GInitableIface  *iface);
53
54 G_DEFINE_TYPE_WITH_CODE (GInetAddressMask, g_inet_address_mask, G_TYPE_OBJECT,
55                          G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
56                                                 g_inet_address_mask_initable_iface_init));
57
58 struct _GInetAddressMaskPrivate
59 {
60   GInetAddress *addr;
61   guint         length;
62 };
63
64 enum
65 {
66   PROP_0,
67   PROP_FAMILY,
68   PROP_ADDRESS,
69   PROP_LENGTH
70 };
71
72 static void
73 g_inet_address_mask_set_property (GObject      *object,
74                                   guint         prop_id,
75                                   const GValue *value,
76                                   GParamSpec   *pspec)
77 {
78   GInetAddressMask *mask = G_INET_ADDRESS_MASK (object);
79
80   switch (prop_id)
81     {
82     case PROP_ADDRESS:
83       if (mask->priv->addr)
84         g_object_unref (mask->priv->addr);
85       mask->priv->addr = g_value_dup_object (value);
86       break;
87
88     case PROP_LENGTH:
89       mask->priv->length = g_value_get_uint (value);
90       break;
91
92     default:
93       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94       break;
95     }
96
97 }
98
99 static void
100 g_inet_address_mask_get_property (GObject    *object,
101                                   guint       prop_id,
102                                   GValue     *value,
103                                   GParamSpec *pspec)
104 {
105   GInetAddressMask *mask = G_INET_ADDRESS_MASK (object);
106
107   switch (prop_id)
108     {
109     case PROP_FAMILY:
110       g_value_set_enum (value, g_inet_address_get_family (mask->priv->addr));
111       break;
112
113     case PROP_ADDRESS:
114       g_value_set_object (value, mask->priv->addr);
115       break;
116
117     case PROP_LENGTH:
118       g_value_set_uint (value, mask->priv->length);
119       break;
120
121     default:
122       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
123     }
124 }
125
126 static void
127 g_inet_address_mask_dispose (GObject *object)
128 {
129   GInetAddressMask *mask = G_INET_ADDRESS_MASK (object);
130
131   g_clear_object (&mask->priv->addr);
132
133   G_OBJECT_CLASS (g_inet_address_mask_parent_class)->dispose (object);
134 }
135
136 static void
137 g_inet_address_mask_class_init (GInetAddressMaskClass *klass)
138 {
139   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
140
141   g_type_class_add_private (klass, sizeof (GInetAddressMaskPrivate));
142
143   gobject_class->set_property = g_inet_address_mask_set_property;
144   gobject_class->get_property = g_inet_address_mask_get_property;
145   gobject_class->dispose = g_inet_address_mask_dispose;
146
147   g_object_class_install_property (gobject_class, PROP_FAMILY,
148                                    g_param_spec_enum ("family",
149                                                       P_("Address family"),
150                                                       P_("The address family (IPv4 or IPv6)"),
151                                                       G_TYPE_SOCKET_FAMILY,
152                                                       G_SOCKET_FAMILY_INVALID,
153                                                       G_PARAM_READABLE |
154                                                       G_PARAM_STATIC_STRINGS));
155   g_object_class_install_property (gobject_class, PROP_ADDRESS,
156                                    g_param_spec_object ("address",
157                                                         P_("Address"),
158                                                         P_("The base address"),
159                                                         G_TYPE_INET_ADDRESS,
160                                                         G_PARAM_READWRITE |
161                                                         G_PARAM_STATIC_STRINGS));
162   g_object_class_install_property (gobject_class, PROP_LENGTH,
163                                    g_param_spec_uint ("length",
164                                                       P_("Length"),
165                                                       P_("The prefix length"),
166                                                       0, 128, 0,
167                                                       G_PARAM_READWRITE |
168                                                       G_PARAM_STATIC_STRINGS));
169 }
170
171 static gboolean
172 g_inet_address_mask_initable_init (GInitable     *initable,
173                                    GCancellable  *cancellable,
174                                    GError       **error)
175 {
176   GInetAddressMask *mask = G_INET_ADDRESS_MASK (initable);
177   guint addrlen, nbytes, nbits;
178   const guint8 *bytes;
179   gboolean ok;
180
181   if (!mask->priv->addr)
182     {
183       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
184                            _("No address specified"));
185       return FALSE;
186     }
187
188   addrlen = g_inet_address_get_native_size (mask->priv->addr);
189   if (mask->priv->length > addrlen * 8)
190     {
191       g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
192                    _("Length %u is too long for address"),
193                    mask->priv->length);
194       return FALSE;
195     }
196
197   /* Make sure all the bits after @length are 0 */
198   bytes = g_inet_address_to_bytes (mask->priv->addr);
199   ok = TRUE;
200
201   nbytes = mask->priv->length / 8;
202   bytes += nbytes;
203   addrlen -= nbytes;
204
205   nbits = mask->priv->length % 8;
206   if (nbits)
207     {
208       if (bytes[0] & (0xFF >> nbits))
209         ok = FALSE;
210       bytes++;
211       addrlen--;
212     }
213
214   while (addrlen)
215     {
216       if (bytes[0])
217         ok = FALSE;
218       bytes++;
219       addrlen--;
220     }
221
222   if (!ok)
223     {
224       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
225                            _("Address has bits set beyond prefix length"));
226       return FALSE;
227     }
228
229   return TRUE;
230 }
231
232 static void
233 g_inet_address_mask_initable_iface_init (GInitableIface  *iface)
234 {
235   iface->init = g_inet_address_mask_initable_init;
236 }
237
238 static void
239 g_inet_address_mask_init (GInetAddressMask *mask)
240 {
241   mask->priv = G_TYPE_INSTANCE_GET_PRIVATE (mask,
242                                             G_TYPE_INET_ADDRESS_MASK,
243                                             GInetAddressMaskPrivate);
244 }
245
246 /**
247  * g_inet_address_mask_new:
248  * @addr: a #GInetAddress
249  * @length: number of bits of @addr to use
250  * @error: return location for #GError, or %NULL
251  *
252  * Creates a new #GInetAddressMask representing all addresses whose
253  * first @length bits match @addr.
254  *
255  * Returns: a new #GInetAddressMask, or %NULL on error
256  *
257  * Since: 2.32
258  */
259 GInetAddressMask *
260 g_inet_address_mask_new (GInetAddress  *addr,
261                          guint          length,
262                          GError       **error)
263 {
264   return g_initable_new (G_TYPE_INET_ADDRESS_MASK, NULL, error,
265                          "address", addr,
266                          "length", length,
267                          NULL);
268 }
269
270 /**
271  * g_inet_address_mask_new_from_string:
272  * @mask_string: an IP address or address/length string
273  * @error: return location for #GError, or %NULL
274  *
275  * Parses @mask_string as an IP address and (optional) length, and
276  * creates a new #GInetAddressMask. The length, if present, is
277  * delimited by a "/". If it is not present, then the length is
278  * assumed to be the full length of the address.
279  *
280  * Returns: a new #GInetAddressMask corresponding to @string, or %NULL
281  * on error.
282  *
283  * Since: 2.32
284  */
285 GInetAddressMask *
286 g_inet_address_mask_new_from_string (const gchar  *mask_string,
287                                      GError      **error)
288 {
289   GInetAddressMask *mask;
290   GInetAddress *addr;
291   gchar *slash;
292   guint length;
293
294   slash = strchr (mask_string, '/');
295   if (slash)
296     {
297       gchar *address, *end;
298
299       length = strtoul (slash + 1, &end, 10);
300       if (*end || !*(slash + 1))
301         {
302         parse_error:
303           g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
304                        _("Could not parse '%s' as IP address mask"),
305                        mask_string);
306           return NULL;
307         }
308
309       address = g_strndup (mask_string, slash - mask_string);
310       addr = g_inet_address_new_from_string (address);
311       g_free (address);
312
313       if (!addr)
314         goto parse_error;
315     }
316   else
317     {
318       addr = g_inet_address_new_from_string (mask_string);
319       if (!addr)
320         goto parse_error;
321
322       length = g_inet_address_get_native_size (addr) * 8;
323     }
324
325   mask = g_inet_address_mask_new (addr, length, error);
326   g_object_unref (addr);
327
328   return mask;
329 }
330
331 /**
332  * g_inet_address_mask_to_string:
333  * @mask: a #GInetAddressMask
334  *
335  * Converts @mask back to its corresponding string form.
336  *
337  * Return value: a string corresponding to @mask.
338  *
339  * Since: 2.32
340  */
341 gchar *
342 g_inet_address_mask_to_string (GInetAddressMask *mask)
343 {
344   gchar *addr_string, *mask_string;
345
346   g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), NULL);
347
348   addr_string = g_inet_address_to_string (mask->priv->addr);
349
350   if (mask->priv->length == (g_inet_address_get_native_size (mask->priv->addr) * 8))
351     return addr_string;
352
353   mask_string = g_strdup_printf ("%s/%u", addr_string, mask->priv->length);
354   g_free (addr_string);
355
356   return mask_string;
357 }
358
359 /**
360  * g_inet_address_mask_get_family:
361  * @mask: a #GInetAddressMask
362  *
363  * Gets the #GSocketFamily of @mask's address
364  *
365  * Return value: the #GSocketFamily of @mask's address
366  *
367  * Since: 2.32
368  */
369 GSocketFamily
370 g_inet_address_mask_get_family (GInetAddressMask *mask)
371 {
372   g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), G_SOCKET_FAMILY_INVALID);
373
374   return g_inet_address_get_family (mask->priv->addr);
375 }
376
377 /**
378  * g_inet_address_mask_get_address:
379  * @mask: a #GInetAddressMask
380  *
381  * Gets @mask's base address
382  *
383  * Return value: (transfer none): @mask's base address
384  *
385  * Since: 2.32
386  */
387 GInetAddress *
388 g_inet_address_mask_get_address (GInetAddressMask *mask)
389 {
390   g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), NULL);
391
392   return mask->priv->addr;
393 }
394
395 /**
396  * g_inet_address_mask_get_length:
397  * @mask: a #GInetAddressMask
398  *
399  * Gets @mask's length
400  *
401  * Return value: @mask's length
402  *
403  * Since: 2.32
404  */
405 guint
406 g_inet_address_mask_get_length (GInetAddressMask *mask)
407 {
408   g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), 0);
409
410   return mask->priv->length;
411 }
412
413 /**
414  * g_inet_address_mask_matches:
415  * @mask: a #GInetAddressMask
416  * @address: a #GInetAddress
417  *
418  * Tests if @address falls within the range described by @mask.
419  *
420  * Return value: whether @address falls within the range described by
421  * @mask.
422  *
423  * Since: 2.32
424  */
425 gboolean
426 g_inet_address_mask_matches (GInetAddressMask *mask,
427                              GInetAddress     *address)
428 {
429   const guint8 *maskbytes, *addrbytes;
430   int nbytes, nbits;
431
432   g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), FALSE);
433   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
434
435   if (g_inet_address_get_family (mask->priv->addr) !=
436       g_inet_address_get_family (address))
437     return FALSE;
438
439   if (mask->priv->length == 0)
440     return TRUE;
441
442   maskbytes = g_inet_address_to_bytes (mask->priv->addr);
443   addrbytes = g_inet_address_to_bytes (address);
444
445   nbytes = mask->priv->length / 8;
446   if (nbytes != 0 && memcmp (maskbytes, addrbytes, nbytes) != 0)
447     return FALSE;
448
449   nbits = mask->priv->length % 8;
450   if (nbits == 0)
451     return TRUE;
452
453   return maskbytes[nbytes] == (addrbytes[nbytes] & (0xFF << (8 - nbits)));
454 }
455
456
457 /**
458  * g_inet_address_mask_equal:
459  * @mask: a #GInetAddressMask
460  * @mask2: another #GInetAddressMask
461  *
462  * Tests if @mask and @mask2 are the same mask.
463  *
464  * Return value: whether @mask and @mask2 are the same mask
465  *
466  * Since: 2.32
467  */
468 gboolean
469 g_inet_address_mask_equal (GInetAddressMask  *mask,
470                            GInetAddressMask  *mask2)
471 {
472   return ((mask->priv->length == mask2->priv->length) &&
473           g_inet_address_equal (mask->priv->addr, mask2->priv->addr));
474 }