1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright 2011 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include "ginetaddressmask.h"
27 #include "ginetaddress.h"
28 #include "ginitable.h"
30 #include "gioenumtypes.h"
36 * `GInetAddressMask` represents a range of IPv4 or IPv6 addresses
37 * described by a base address and a length indicating how many bits
38 * of the base address are relevant for matching purposes. These are
39 * often given in string form. For example, `10.0.0.0/8`, or `fe80::/10`.
44 struct _GInetAddressMaskPrivate
50 static void g_inet_address_mask_initable_iface_init (GInitableIface *iface);
52 G_DEFINE_TYPE_WITH_CODE (GInetAddressMask, g_inet_address_mask, G_TYPE_OBJECT,
53 G_ADD_PRIVATE (GInetAddressMask)
54 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
55 g_inet_address_mask_initable_iface_init))
66 g_inet_address_mask_set_property (GObject *object,
71 GInetAddressMask *mask = G_INET_ADDRESS_MASK (object);
77 g_object_unref (mask->priv->addr);
78 mask->priv->addr = g_value_dup_object (value);
82 mask->priv->length = g_value_get_uint (value);
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 g_inet_address_mask_get_property (GObject *object,
98 GInetAddressMask *mask = G_INET_ADDRESS_MASK (object);
103 g_value_set_enum (value, g_inet_address_get_family (mask->priv->addr));
107 g_value_set_object (value, mask->priv->addr);
111 g_value_set_uint (value, mask->priv->length);
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
120 g_inet_address_mask_dispose (GObject *object)
122 GInetAddressMask *mask = G_INET_ADDRESS_MASK (object);
124 g_clear_object (&mask->priv->addr);
126 G_OBJECT_CLASS (g_inet_address_mask_parent_class)->dispose (object);
130 g_inet_address_mask_class_init (GInetAddressMaskClass *klass)
132 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
134 gobject_class->set_property = g_inet_address_mask_set_property;
135 gobject_class->get_property = g_inet_address_mask_get_property;
136 gobject_class->dispose = g_inet_address_mask_dispose;
139 * GInetAddressMask:family:
141 * The address family (IPv4 or IPv6).
145 g_object_class_install_property (gobject_class, PROP_FAMILY,
146 g_param_spec_enum ("family", NULL, NULL,
147 G_TYPE_SOCKET_FAMILY,
148 G_SOCKET_FAMILY_INVALID,
150 G_PARAM_STATIC_STRINGS));
153 * GInetAddressMask:address:
159 g_object_class_install_property (gobject_class, PROP_ADDRESS,
160 g_param_spec_object ("address", NULL, NULL,
163 G_PARAM_STATIC_STRINGS));
166 * GInetAddressMask:length:
168 * The prefix length, in bytes.
172 g_object_class_install_property (gobject_class, PROP_LENGTH,
173 g_param_spec_uint ("length", NULL, NULL,
176 G_PARAM_STATIC_STRINGS));
180 g_inet_address_mask_initable_init (GInitable *initable,
181 GCancellable *cancellable,
184 GInetAddressMask *mask = G_INET_ADDRESS_MASK (initable);
185 guint addrlen, nbytes, nbits;
189 if (!mask->priv->addr)
191 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
192 _("No address specified"));
196 addrlen = g_inet_address_get_native_size (mask->priv->addr);
197 if (mask->priv->length > addrlen * 8)
199 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
200 _("Length %u is too long for address"),
205 /* Make sure all the bits after @length are 0 */
206 bytes = g_inet_address_to_bytes (mask->priv->addr);
209 nbytes = mask->priv->length / 8;
213 nbits = mask->priv->length % 8;
216 if (bytes[0] & (0xFF >> nbits))
232 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
233 _("Address has bits set beyond prefix length"));
241 g_inet_address_mask_initable_iface_init (GInitableIface *iface)
243 iface->init = g_inet_address_mask_initable_init;
247 g_inet_address_mask_init (GInetAddressMask *mask)
249 mask->priv = g_inet_address_mask_get_instance_private (mask);
253 * g_inet_address_mask_new:
254 * @addr: a #GInetAddress
255 * @length: number of bits of @addr to use
256 * @error: return location for #GError, or %NULL
258 * Creates a new #GInetAddressMask representing all addresses whose
259 * first @length bits match @addr.
261 * Returns: a new #GInetAddressMask, or %NULL on error
266 g_inet_address_mask_new (GInetAddress *addr,
270 return g_initable_new (G_TYPE_INET_ADDRESS_MASK, NULL, error,
277 * g_inet_address_mask_new_from_string:
278 * @mask_string: an IP address or address/length string
279 * @error: return location for #GError, or %NULL
281 * Parses @mask_string as an IP address and (optional) length, and
282 * creates a new #GInetAddressMask. The length, if present, is
283 * delimited by a "/". If it is not present, then the length is
284 * assumed to be the full length of the address.
286 * Returns: a new #GInetAddressMask corresponding to @string, or %NULL
292 g_inet_address_mask_new_from_string (const gchar *mask_string,
295 GInetAddressMask *mask;
300 slash = strchr (mask_string, '/');
303 gchar *address, *end;
305 length = strtoul (slash + 1, &end, 10);
306 if (*end || !*(slash + 1))
309 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
310 _("Could not parse ā%sā as IP address mask"),
315 address = g_strndup (mask_string, slash - mask_string);
316 addr = g_inet_address_new_from_string (address);
324 addr = g_inet_address_new_from_string (mask_string);
328 length = g_inet_address_get_native_size (addr) * 8;
331 mask = g_inet_address_mask_new (addr, length, error);
332 g_object_unref (addr);
338 * g_inet_address_mask_to_string:
339 * @mask: a #GInetAddressMask
341 * Converts @mask back to its corresponding string form.
343 * Returns: a string corresponding to @mask.
348 g_inet_address_mask_to_string (GInetAddressMask *mask)
350 gchar *addr_string, *mask_string;
352 g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), NULL);
354 addr_string = g_inet_address_to_string (mask->priv->addr);
356 if (mask->priv->length == (g_inet_address_get_native_size (mask->priv->addr) * 8))
359 mask_string = g_strdup_printf ("%s/%u", addr_string, mask->priv->length);
360 g_free (addr_string);
366 * g_inet_address_mask_get_family:
367 * @mask: a #GInetAddressMask
369 * Gets the #GSocketFamily of @mask's address
371 * Returns: the #GSocketFamily of @mask's address
376 g_inet_address_mask_get_family (GInetAddressMask *mask)
378 g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), G_SOCKET_FAMILY_INVALID);
380 return g_inet_address_get_family (mask->priv->addr);
384 * g_inet_address_mask_get_address:
385 * @mask: a #GInetAddressMask
387 * Gets @mask's base address
389 * Returns: (transfer none): @mask's base address
394 g_inet_address_mask_get_address (GInetAddressMask *mask)
396 g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), NULL);
398 return mask->priv->addr;
402 * g_inet_address_mask_get_length:
403 * @mask: a #GInetAddressMask
405 * Gets @mask's length
407 * Returns: @mask's length
412 g_inet_address_mask_get_length (GInetAddressMask *mask)
414 g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), 0);
416 return mask->priv->length;
420 * g_inet_address_mask_matches:
421 * @mask: a #GInetAddressMask
422 * @address: a #GInetAddress
424 * Tests if @address falls within the range described by @mask.
426 * Returns: whether @address falls within the range described by
432 g_inet_address_mask_matches (GInetAddressMask *mask,
433 GInetAddress *address)
435 const guint8 *maskbytes, *addrbytes;
438 g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), FALSE);
439 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
441 if (g_inet_address_get_family (mask->priv->addr) !=
442 g_inet_address_get_family (address))
445 if (mask->priv->length == 0)
448 maskbytes = g_inet_address_to_bytes (mask->priv->addr);
449 addrbytes = g_inet_address_to_bytes (address);
451 nbytes = mask->priv->length / 8;
452 if (nbytes != 0 && memcmp (maskbytes, addrbytes, nbytes) != 0)
455 nbits = mask->priv->length % 8;
459 return maskbytes[nbytes] == (addrbytes[nbytes] & (0xFF << (8 - nbits)));
464 * g_inet_address_mask_equal:
465 * @mask: a #GInetAddressMask
466 * @mask2: another #GInetAddressMask
468 * Tests if @mask and @mask2 are the same mask.
470 * Returns: whether @mask and @mask2 are the same mask
475 g_inet_address_mask_equal (GInetAddressMask *mask,
476 GInetAddressMask *mask2)
478 g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask), FALSE);
479 g_return_val_if_fail (G_IS_INET_ADDRESS_MASK (mask2), FALSE);
481 return ((mask->priv->length == mask2->priv->length) &&
482 g_inet_address_equal (mask->priv->addr, mask2->priv->addr));