GSettings: small internal refactor
[glib.git] / gio / ginetaddress.c
blob2db01cb266ca8ecf441fe862c2da3963fec056fe
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
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.
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.
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.
20 * Authors: Christian Kellner <gicmo@gnome.org>
21 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
24 #include <config.h>
26 #include <string.h>
28 #include <glib.h>
30 #include "ginetaddress.h"
31 #include "gioenums.h"
32 #include "gioenumtypes.h"
33 #include "glibintl.h"
34 #include "gnetworkingprivate.h"
36 struct _GInetAddressPrivate
38 GSocketFamily family;
39 union {
40 struct in_addr ipv4;
41 struct in6_addr ipv6;
42 } addr;
45 /**
46 * SECTION:ginetaddress
47 * @short_description: An IPv4/IPv6 address
49 * #GInetAddress represents an IPv4 or IPv6 internet address. Use
50 * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_async() to
51 * look up the #GInetAddress for a hostname. Use
52 * g_resolver_lookup_by_address() or
53 * g_resolver_lookup_by_address_async() to look up the hostname for a
54 * #GInetAddress.
56 * To actually connect to a remote host, you will need a
57 * #GInetSocketAddress (which includes a #GInetAddress as well as a
58 * port number).
61 /**
62 * GInetAddress:
64 * An IPv4 or IPv6 internet address.
67 G_DEFINE_TYPE_WITH_CODE (GInetAddress, g_inet_address, G_TYPE_OBJECT,
68 G_ADD_PRIVATE (GInetAddress)
69 g_networking_init ();)
71 enum
73 PROP_0,
74 PROP_FAMILY,
75 PROP_BYTES,
76 PROP_IS_ANY,
77 PROP_IS_LOOPBACK,
78 PROP_IS_LINK_LOCAL,
79 PROP_IS_SITE_LOCAL,
80 PROP_IS_MULTICAST,
81 PROP_IS_MC_GLOBAL,
82 PROP_IS_MC_LINK_LOCAL,
83 PROP_IS_MC_NODE_LOCAL,
84 PROP_IS_MC_ORG_LOCAL,
85 PROP_IS_MC_SITE_LOCAL,
88 static void
89 g_inet_address_set_property (GObject *object,
90 guint prop_id,
91 const GValue *value,
92 GParamSpec *pspec)
94 GInetAddress *address = G_INET_ADDRESS (object);
96 switch (prop_id)
98 case PROP_FAMILY:
99 address->priv->family = g_value_get_enum (value);
100 break;
102 case PROP_BYTES:
103 memcpy (&address->priv->addr, g_value_get_pointer (value),
104 address->priv->family == AF_INET ?
105 sizeof (address->priv->addr.ipv4) :
106 sizeof (address->priv->addr.ipv6));
107 break;
109 default:
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111 break;
116 static void
117 g_inet_address_get_property (GObject *object,
118 guint prop_id,
119 GValue *value,
120 GParamSpec *pspec)
122 GInetAddress *address = G_INET_ADDRESS (object);
124 switch (prop_id)
126 case PROP_FAMILY:
127 g_value_set_enum (value, address->priv->family);
128 break;
130 case PROP_BYTES:
131 g_value_set_pointer (value, &address->priv->addr);
132 break;
134 case PROP_IS_ANY:
135 g_value_set_boolean (value, g_inet_address_get_is_any (address));
136 break;
138 case PROP_IS_LOOPBACK:
139 g_value_set_boolean (value, g_inet_address_get_is_loopback (address));
140 break;
142 case PROP_IS_LINK_LOCAL:
143 g_value_set_boolean (value, g_inet_address_get_is_link_local (address));
144 break;
146 case PROP_IS_SITE_LOCAL:
147 g_value_set_boolean (value, g_inet_address_get_is_site_local (address));
148 break;
150 case PROP_IS_MULTICAST:
151 g_value_set_boolean (value, g_inet_address_get_is_multicast (address));
152 break;
154 case PROP_IS_MC_GLOBAL:
155 g_value_set_boolean (value, g_inet_address_get_is_mc_global (address));
156 break;
158 case PROP_IS_MC_LINK_LOCAL:
159 g_value_set_boolean (value, g_inet_address_get_is_mc_link_local (address));
160 break;
162 case PROP_IS_MC_NODE_LOCAL:
163 g_value_set_boolean (value, g_inet_address_get_is_mc_node_local (address));
164 break;
166 case PROP_IS_MC_ORG_LOCAL:
167 g_value_set_boolean (value, g_inet_address_get_is_mc_org_local (address));
168 break;
170 case PROP_IS_MC_SITE_LOCAL:
171 g_value_set_boolean (value, g_inet_address_get_is_mc_site_local (address));
172 break;
174 default:
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 static void
180 g_inet_address_class_init (GInetAddressClass *klass)
182 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
184 gobject_class->set_property = g_inet_address_set_property;
185 gobject_class->get_property = g_inet_address_get_property;
187 g_object_class_install_property (gobject_class, PROP_FAMILY,
188 g_param_spec_enum ("family",
189 P_("Address family"),
190 P_("The address family (IPv4 or IPv6)"),
191 G_TYPE_SOCKET_FAMILY,
192 G_SOCKET_FAMILY_INVALID,
193 G_PARAM_READWRITE |
194 G_PARAM_CONSTRUCT_ONLY |
195 G_PARAM_STATIC_STRINGS));
197 g_object_class_install_property (gobject_class, PROP_BYTES,
198 g_param_spec_pointer ("bytes",
199 P_("Bytes"),
200 P_("The raw address data"),
201 G_PARAM_READWRITE |
202 G_PARAM_CONSTRUCT_ONLY |
203 G_PARAM_STATIC_STRINGS));
206 * GInetAddress:is-any:
208 * Whether this is the "any" address for its family.
209 * See g_inet_address_get_is_any().
211 * Since: 2.22
213 g_object_class_install_property (gobject_class, PROP_IS_ANY,
214 g_param_spec_boolean ("is-any",
215 P_("Is any"),
216 P_("Whether this is the \"any\" address for its family"),
217 FALSE,
218 G_PARAM_READABLE |
219 G_PARAM_STATIC_STRINGS));
222 * GInetAddress:is-link-local:
224 * Whether this is a link-local address.
225 * See g_inet_address_get_is_link_local().
227 * Since: 2.22
229 g_object_class_install_property (gobject_class, PROP_IS_LINK_LOCAL,
230 g_param_spec_boolean ("is-link-local",
231 P_("Is link-local"),
232 P_("Whether this is a link-local address"),
233 FALSE,
234 G_PARAM_READABLE |
235 G_PARAM_STATIC_STRINGS));
238 * GInetAddress:is-loopback:
240 * Whether this is the loopback address for its family.
241 * See g_inet_address_get_is_loopback().
243 * Since: 2.22
245 g_object_class_install_property (gobject_class, PROP_IS_LOOPBACK,
246 g_param_spec_boolean ("is-loopback",
247 P_("Is loopback"),
248 P_("Whether this is the loopback address for its family"),
249 FALSE,
250 G_PARAM_READABLE |
251 G_PARAM_STATIC_STRINGS));
254 * GInetAddress:is-site-local:
256 * Whether this is a site-local address.
257 * See g_inet_address_get_is_loopback().
259 * Since: 2.22
261 g_object_class_install_property (gobject_class, PROP_IS_SITE_LOCAL,
262 g_param_spec_boolean ("is-site-local",
263 P_("Is site-local"),
264 P_("Whether this is a site-local address"),
265 FALSE,
266 G_PARAM_READABLE |
267 G_PARAM_STATIC_STRINGS));
270 * GInetAddress:is-multicast:
272 * Whether this is a multicast address.
273 * See g_inet_address_get_is_multicast().
275 * Since: 2.22
277 g_object_class_install_property (gobject_class, PROP_IS_MULTICAST,
278 g_param_spec_boolean ("is-multicast",
279 P_("Is multicast"),
280 P_("Whether this is a multicast address"),
281 FALSE,
282 G_PARAM_READABLE |
283 G_PARAM_STATIC_STRINGS));
286 * GInetAddress:is-mc-global:
288 * Whether this is a global multicast address.
289 * See g_inet_address_get_is_mc_global().
291 * Since: 2.22
293 g_object_class_install_property (gobject_class, PROP_IS_MC_GLOBAL,
294 g_param_spec_boolean ("is-mc-global",
295 P_("Is multicast global"),
296 P_("Whether this is a global multicast address"),
297 FALSE,
298 G_PARAM_READABLE |
299 G_PARAM_STATIC_STRINGS));
303 * GInetAddress:is-mc-link-local:
305 * Whether this is a link-local multicast address.
306 * See g_inet_address_get_is_mc_link_local().
308 * Since: 2.22
310 g_object_class_install_property (gobject_class, PROP_IS_MC_LINK_LOCAL,
311 g_param_spec_boolean ("is-mc-link-local",
312 P_("Is multicast link-local"),
313 P_("Whether this is a link-local multicast address"),
314 FALSE,
315 G_PARAM_READABLE |
316 G_PARAM_STATIC_STRINGS));
319 * GInetAddress:is-mc-node-local:
321 * Whether this is a node-local multicast address.
322 * See g_inet_address_get_is_mc_node_local().
324 * Since: 2.22
326 g_object_class_install_property (gobject_class, PROP_IS_MC_NODE_LOCAL,
327 g_param_spec_boolean ("is-mc-node-local",
328 P_("Is multicast node-local"),
329 P_("Whether this is a node-local multicast address"),
330 FALSE,
331 G_PARAM_READABLE |
332 G_PARAM_STATIC_STRINGS));
335 * GInetAddress:is-mc-org-local:
337 * Whether this is an organization-local multicast address.
338 * See g_inet_address_get_is_mc_org_local().
340 * Since: 2.22
342 g_object_class_install_property (gobject_class, PROP_IS_MC_ORG_LOCAL,
343 g_param_spec_boolean ("is-mc-org-local",
344 P_("Is multicast org-local"),
345 P_("Whether this is an organization-local multicast address"),
346 FALSE,
347 G_PARAM_READABLE |
348 G_PARAM_STATIC_STRINGS));
351 * GInetAddress:is-mc-site-local:
353 * Whether this is a site-local multicast address.
354 * See g_inet_address_get_is_mc_site_local().
356 * Since: 2.22
358 g_object_class_install_property (gobject_class, PROP_IS_MC_SITE_LOCAL,
359 g_param_spec_boolean ("is-mc-site-local",
360 P_("Is multicast site-local"),
361 P_("Whether this is a site-local multicast address"),
362 FALSE,
363 G_PARAM_READABLE |
364 G_PARAM_STATIC_STRINGS));
367 static void
368 g_inet_address_init (GInetAddress *address)
370 address->priv = g_inet_address_get_instance_private (address);
374 * g_inet_address_new_from_string:
375 * @string: a string representation of an IP address
377 * Parses @string as an IP address and creates a new #GInetAddress.
379 * Returns: a new #GInetAddress corresponding to @string, or %NULL if
380 * @string could not be parsed.
382 * Since: 2.22
384 GInetAddress *
385 g_inet_address_new_from_string (const gchar *string)
387 #ifdef G_OS_WIN32
388 struct sockaddr_storage sa;
389 struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
390 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
391 gint len;
392 #else /* !G_OS_WIN32 */
393 struct in_addr in_addr;
394 struct in6_addr in6_addr;
395 #endif
397 /* If this GInetAddress is the first networking-related object to be
398 * created, then we won't have called g_networking_init() yet at
399 * this point.
401 g_networking_init ();
403 #ifdef G_OS_WIN32
404 /* We need to make sure to not pass a string of the form
405 * "IPv4addr:port" or "[IPv6addr]:port" to WSAStringToAddress(),
406 * since it would accept them (returning both the address and the
407 * port), but we only want to accept standalone IP addresses. (In
408 * the IPv6 case, WINE actually only checks for the ']', not the
409 * '[', which is why we do the same here.)
411 if (!strchr (string, ':'))
413 len = sizeof (sa);
414 if (WSAStringToAddress ((LPTSTR) string, AF_INET, NULL, (LPSOCKADDR) &sa, &len) == 0)
415 return g_inet_address_new_from_bytes ((guint8 *)&sin->sin_addr, AF_INET);
418 if (!strchr (string, ']'))
420 len = sizeof (sa);
421 if (WSAStringToAddress ((LPTSTR) string, AF_INET6, NULL, (LPSOCKADDR) &sa, &len) == 0)
422 return g_inet_address_new_from_bytes ((guint8 *)&sin6->sin6_addr, AF_INET6);
425 #else /* !G_OS_WIN32 */
427 if (inet_pton (AF_INET, string, &in_addr) > 0)
428 return g_inet_address_new_from_bytes ((guint8 *)&in_addr, AF_INET);
429 else if (inet_pton (AF_INET6, string, &in6_addr) > 0)
430 return g_inet_address_new_from_bytes ((guint8 *)&in6_addr, AF_INET6);
431 #endif
433 return NULL;
436 #define G_INET_ADDRESS_FAMILY_IS_VALID(family) ((family) == AF_INET || (family) == AF_INET6)
439 * g_inet_address_new_from_bytes:
440 * @bytes: (array) (element-type guint8): raw address data
441 * @family: the address family of @bytes
443 * Creates a new #GInetAddress from the given @family and @bytes.
444 * @bytes should be 4 bytes for %G_SOCKET_FAMILY_IPV4 and 16 bytes for
445 * %G_SOCKET_FAMILY_IPV6.
447 * Returns: a new #GInetAddress corresponding to @family and @bytes.
449 * Since: 2.22
451 GInetAddress *
452 g_inet_address_new_from_bytes (const guint8 *bytes,
453 GSocketFamily family)
455 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
457 return g_object_new (G_TYPE_INET_ADDRESS,
458 "family", family,
459 "bytes", bytes,
460 NULL);
464 * g_inet_address_new_loopback:
465 * @family: the address family
467 * Creates a #GInetAddress for the loopback address for @family.
469 * Returns: a new #GInetAddress corresponding to the loopback address
470 * for @family.
472 * Since: 2.22
474 GInetAddress *
475 g_inet_address_new_loopback (GSocketFamily family)
477 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
479 if (family == AF_INET)
481 guint8 addr[4] = {127, 0, 0, 1};
483 return g_inet_address_new_from_bytes (addr, family);
485 else
486 return g_inet_address_new_from_bytes (in6addr_loopback.s6_addr, family);
490 * g_inet_address_new_any:
491 * @family: the address family
493 * Creates a #GInetAddress for the "any" address (unassigned/"don't
494 * care") for @family.
496 * Returns: a new #GInetAddress corresponding to the "any" address
497 * for @family.
499 * Since: 2.22
501 GInetAddress *
502 g_inet_address_new_any (GSocketFamily family)
504 g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
506 if (family == AF_INET)
508 guint8 addr[4] = {0, 0, 0, 0};
510 return g_inet_address_new_from_bytes (addr, family);
512 else
513 return g_inet_address_new_from_bytes (in6addr_any.s6_addr, family);
518 * g_inet_address_to_string:
519 * @address: a #GInetAddress
521 * Converts @address to string form.
523 * Returns: a representation of @address as a string, which should be
524 * freed after use.
526 * Since: 2.22
528 gchar *
529 g_inet_address_to_string (GInetAddress *address)
531 gchar buffer[INET6_ADDRSTRLEN];
532 #ifdef G_OS_WIN32
533 DWORD buflen = sizeof (buffer), addrlen;
534 struct sockaddr_storage sa;
535 struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
536 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
537 #endif
539 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
541 #ifdef G_OS_WIN32
542 memset (&sa, 0, sizeof (sa));
543 sa.ss_family = address->priv->family;
544 if (address->priv->family == AF_INET)
546 addrlen = sizeof (*sin);
547 memcpy (&sin->sin_addr, &address->priv->addr.ipv4,
548 sizeof (sin->sin_addr));
550 else
552 addrlen = sizeof (*sin6);
553 memcpy (&sin6->sin6_addr, &address->priv->addr.ipv6,
554 sizeof (sin6->sin6_addr));
556 if (WSAAddressToString ((LPSOCKADDR) &sa, addrlen, NULL, buffer, &buflen) != 0)
557 return NULL;
559 #else /* !G_OS_WIN32 */
561 if (address->priv->family == AF_INET)
562 inet_ntop (AF_INET, &address->priv->addr.ipv4, buffer, sizeof (buffer));
563 else
564 inet_ntop (AF_INET6, &address->priv->addr.ipv6, buffer, sizeof (buffer));
565 #endif
567 return g_strdup (buffer);
571 * g_inet_address_to_bytes: (skip)
572 * @address: a #GInetAddress
574 * Gets the raw binary address data from @address.
576 * Returns: a pointer to an internal array of the bytes in @address,
577 * which should not be modified, stored, or freed. The size of this
578 * array can be gotten with g_inet_address_get_native_size().
580 * Since: 2.22
582 const guint8 *
583 g_inet_address_to_bytes (GInetAddress *address)
585 g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
587 return (guint8 *)&address->priv->addr;
591 * g_inet_address_get_native_size:
592 * @address: a #GInetAddress
594 * Gets the size of the native raw binary address for @address. This
595 * is the size of the data that you get from g_inet_address_to_bytes().
597 * Returns: the number of bytes used for the native version of @address.
599 * Since: 2.22
601 gsize
602 g_inet_address_get_native_size (GInetAddress *address)
604 if (address->priv->family == AF_INET)
605 return sizeof (address->priv->addr.ipv4);
606 return sizeof (address->priv->addr.ipv6);
610 * g_inet_address_get_family:
611 * @address: a #GInetAddress
613 * Gets @address's family
615 * Returns: @address's family
617 * Since: 2.22
619 GSocketFamily
620 g_inet_address_get_family (GInetAddress *address)
622 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
624 return address->priv->family;
628 * g_inet_address_get_is_any:
629 * @address: a #GInetAddress
631 * Tests whether @address is the "any" address for its family.
633 * Returns: %TRUE if @address is the "any" address for its family.
635 * Since: 2.22
637 gboolean
638 g_inet_address_get_is_any (GInetAddress *address)
640 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
642 if (address->priv->family == AF_INET)
644 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
646 return addr4 == INADDR_ANY;
648 else
649 return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6);
653 * g_inet_address_get_is_loopback:
654 * @address: a #GInetAddress
656 * Tests whether @address is the loopback address for its family.
658 * Returns: %TRUE if @address is the loopback address for its family.
660 * Since: 2.22
662 gboolean
663 g_inet_address_get_is_loopback (GInetAddress *address)
665 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
667 if (address->priv->family == AF_INET)
669 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
671 /* 127.0.0.0/8 */
672 return ((addr4 & 0xff000000) == 0x7f000000);
674 else
675 return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6);
679 * g_inet_address_get_is_link_local:
680 * @address: a #GInetAddress
682 * Tests whether @address is a link-local address (that is, if it
683 * identifies a host on a local network that is not connected to the
684 * Internet).
686 * Returns: %TRUE if @address is a link-local address.
688 * Since: 2.22
690 gboolean
691 g_inet_address_get_is_link_local (GInetAddress *address)
693 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
695 if (address->priv->family == AF_INET)
697 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
699 /* 169.254.0.0/16 */
700 return ((addr4 & 0xffff0000) == 0xa9fe0000);
702 else
703 return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6);
707 * g_inet_address_get_is_site_local:
708 * @address: a #GInetAddress
710 * Tests whether @address is a site-local address such as 10.0.0.1
711 * (that is, the address identifies a host on a local network that can
712 * not be reached directly from the Internet, but which may have
713 * outgoing Internet connectivity via a NAT or firewall).
715 * Returns: %TRUE if @address is a site-local address.
717 * Since: 2.22
719 gboolean
720 g_inet_address_get_is_site_local (GInetAddress *address)
722 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
724 if (address->priv->family == AF_INET)
726 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
728 /* 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 */
729 return ((addr4 & 0xff000000) == 0x0a000000 ||
730 (addr4 & 0xfff00000) == 0xac100000 ||
731 (addr4 & 0xffff0000) == 0xc0a80000);
733 else
734 return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6);
738 * g_inet_address_get_is_multicast:
739 * @address: a #GInetAddress
741 * Tests whether @address is a multicast address.
743 * Returns: %TRUE if @address is a multicast address.
745 * Since: 2.22
747 gboolean
748 g_inet_address_get_is_multicast (GInetAddress *address)
750 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
752 if (address->priv->family == AF_INET)
754 guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
756 return IN_MULTICAST (addr4);
758 else
759 return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6);
763 * g_inet_address_get_is_mc_global:
764 * @address: a #GInetAddress
766 * Tests whether @address is a global multicast address.
768 * Returns: %TRUE if @address is a global multicast address.
770 * Since: 2.22
772 gboolean
773 g_inet_address_get_is_mc_global (GInetAddress *address)
775 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
777 if (address->priv->family == AF_INET)
778 return FALSE;
779 else
780 return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6);
784 * g_inet_address_get_is_mc_link_local:
785 * @address: a #GInetAddress
787 * Tests whether @address is a link-local multicast address.
789 * Returns: %TRUE if @address is a link-local multicast address.
791 * Since: 2.22
793 gboolean
794 g_inet_address_get_is_mc_link_local (GInetAddress *address)
796 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
798 if (address->priv->family == AF_INET)
799 return FALSE;
800 else
801 return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6);
805 * g_inet_address_get_is_mc_node_local:
806 * @address: a #GInetAddress
808 * Tests whether @address is a node-local multicast address.
810 * Returns: %TRUE if @address is a node-local multicast address.
812 * Since: 2.22
814 gboolean
815 g_inet_address_get_is_mc_node_local (GInetAddress *address)
817 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
819 if (address->priv->family == AF_INET)
820 return FALSE;
821 else
822 return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6);
826 * g_inet_address_get_is_mc_org_local:
827 * @address: a #GInetAddress
829 * Tests whether @address is an organization-local multicast address.
831 * Returns: %TRUE if @address is an organization-local multicast address.
833 * Since: 2.22
835 gboolean
836 g_inet_address_get_is_mc_org_local (GInetAddress *address)
838 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
840 if (address->priv->family == AF_INET)
841 return FALSE;
842 else
843 return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6);
847 * g_inet_address_get_is_mc_site_local:
848 * @address: a #GInetAddress
850 * Tests whether @address is a site-local multicast address.
852 * Returns: %TRUE if @address is a site-local multicast address.
854 * Since: 2.22
856 gboolean
857 g_inet_address_get_is_mc_site_local (GInetAddress *address)
859 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
861 if (address->priv->family == AF_INET)
862 return FALSE;
863 else
864 return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6);
868 * g_inet_address_equal:
869 * @address: A #GInetAddress.
870 * @other_address: Another #GInetAddress.
872 * Checks if two #GInetAddress instances are equal, e.g. the same address.
874 * Returns: %TRUE if @address and @other_address are equal, %FALSE otherwise.
876 * Since: 2.30
878 gboolean
879 g_inet_address_equal (GInetAddress *address,
880 GInetAddress *other_address)
882 g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
883 g_return_val_if_fail (G_IS_INET_ADDRESS (other_address), FALSE);
885 if (g_inet_address_get_family (address) != g_inet_address_get_family (other_address))
886 return FALSE;
888 if (memcmp (g_inet_address_to_bytes (address),
889 g_inet_address_to_bytes (other_address),
890 g_inet_address_get_native_size (address)) != 0)
891 return FALSE;
893 return TRUE;