Imported Upstream version 2.66.6
[platform/upstream/glib.git] / gio / ginetaddress.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
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.1 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, see <http://www.gnu.org/licenses/>.
17  *
18  * Authors: Christian Kellner <gicmo@gnome.org>
19  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <glib.h>
27
28 #include "ginetaddress.h"
29 #include "gioenums.h"
30 #include "gioenumtypes.h"
31 #include "glibintl.h"
32 #include "gnetworkingprivate.h"
33
34 struct _GInetAddressPrivate
35 {
36   GSocketFamily family;
37   union {
38     struct in_addr ipv4;
39     struct in6_addr ipv6;
40   } addr;
41 };
42
43 /**
44  * SECTION:ginetaddress
45  * @short_description: An IPv4/IPv6 address
46  * @include: gio/gio.h
47  *
48  * #GInetAddress represents an IPv4 or IPv6 internet address. Use
49  * g_resolver_lookup_by_name() or g_resolver_lookup_by_name_async() to
50  * look up the #GInetAddress for a hostname. Use
51  * g_resolver_lookup_by_address() or
52  * g_resolver_lookup_by_address_async() to look up the hostname for a
53  * #GInetAddress.
54  *
55  * To actually connect to a remote host, you will need a
56  * #GInetSocketAddress (which includes a #GInetAddress as well as a
57  * port number).
58  */
59
60 /**
61  * GInetAddress:
62  *
63  * An IPv4 or IPv6 internet address.
64  */
65
66 G_DEFINE_TYPE_WITH_CODE (GInetAddress, g_inet_address, G_TYPE_OBJECT,
67                          G_ADD_PRIVATE (GInetAddress)
68                          g_networking_init ();)
69
70 enum
71 {
72   PROP_0,
73   PROP_FAMILY,
74   PROP_BYTES,
75   PROP_IS_ANY,
76   PROP_IS_LOOPBACK,
77   PROP_IS_LINK_LOCAL,
78   PROP_IS_SITE_LOCAL,
79   PROP_IS_MULTICAST,
80   PROP_IS_MC_GLOBAL,
81   PROP_IS_MC_LINK_LOCAL,
82   PROP_IS_MC_NODE_LOCAL,
83   PROP_IS_MC_ORG_LOCAL,
84   PROP_IS_MC_SITE_LOCAL,
85 };
86
87 static void
88 g_inet_address_set_property (GObject      *object,
89                              guint         prop_id,
90                              const GValue *value,
91                              GParamSpec   *pspec)
92 {
93   GInetAddress *address = G_INET_ADDRESS (object);
94
95   switch (prop_id)
96     {
97     case PROP_FAMILY:
98       address->priv->family = g_value_get_enum (value);
99       break;
100
101     case PROP_BYTES:
102       memcpy (&address->priv->addr, g_value_get_pointer (value),
103               address->priv->family == AF_INET ?
104               sizeof (address->priv->addr.ipv4) :
105               sizeof (address->priv->addr.ipv6));
106       break;
107
108     default:
109       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
110       break;
111     }
112
113 }
114
115 static void
116 g_inet_address_get_property (GObject    *object,
117                              guint       prop_id,
118                              GValue     *value,
119                              GParamSpec *pspec)
120 {
121   GInetAddress *address = G_INET_ADDRESS (object);
122
123   switch (prop_id)
124     {
125     case PROP_FAMILY:
126       g_value_set_enum (value, address->priv->family);
127       break;
128
129     case PROP_BYTES:
130       g_value_set_pointer (value, &address->priv->addr);
131       break;
132
133     case PROP_IS_ANY:
134       g_value_set_boolean (value, g_inet_address_get_is_any (address));
135       break;
136
137     case PROP_IS_LOOPBACK:
138       g_value_set_boolean (value, g_inet_address_get_is_loopback (address));
139       break;
140
141     case PROP_IS_LINK_LOCAL:
142       g_value_set_boolean (value, g_inet_address_get_is_link_local (address));
143       break;
144
145     case PROP_IS_SITE_LOCAL:
146       g_value_set_boolean (value, g_inet_address_get_is_site_local (address));
147       break;
148
149     case PROP_IS_MULTICAST:
150       g_value_set_boolean (value, g_inet_address_get_is_multicast (address));
151       break;
152
153     case PROP_IS_MC_GLOBAL:
154       g_value_set_boolean (value, g_inet_address_get_is_mc_global (address));
155       break;
156
157     case PROP_IS_MC_LINK_LOCAL:
158       g_value_set_boolean (value, g_inet_address_get_is_mc_link_local (address));
159       break;
160
161     case PROP_IS_MC_NODE_LOCAL:
162       g_value_set_boolean (value, g_inet_address_get_is_mc_node_local (address));
163       break;
164
165     case PROP_IS_MC_ORG_LOCAL:
166       g_value_set_boolean (value, g_inet_address_get_is_mc_org_local (address));
167       break;
168
169     case PROP_IS_MC_SITE_LOCAL:
170       g_value_set_boolean (value, g_inet_address_get_is_mc_site_local (address));
171       break;
172
173     default:
174       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175     }
176 }
177
178 static void
179 g_inet_address_class_init (GInetAddressClass *klass)
180 {
181   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
182
183   gobject_class->set_property = g_inet_address_set_property;
184   gobject_class->get_property = g_inet_address_get_property;
185
186   g_object_class_install_property (gobject_class, PROP_FAMILY,
187                                    g_param_spec_enum ("family",
188                                                       P_("Address family"),
189                                                       P_("The address family (IPv4 or IPv6)"),
190                                                       G_TYPE_SOCKET_FAMILY,
191                                                       G_SOCKET_FAMILY_INVALID,
192                                                       G_PARAM_READWRITE |
193                                                       G_PARAM_CONSTRUCT_ONLY |
194                                                       G_PARAM_STATIC_STRINGS));
195
196   g_object_class_install_property (gobject_class, PROP_BYTES,
197                                    g_param_spec_pointer ("bytes",
198                                                          P_("Bytes"),
199                                                          P_("The raw address data"),
200                                                          G_PARAM_READWRITE |
201                                                          G_PARAM_CONSTRUCT_ONLY |
202                                                          G_PARAM_STATIC_STRINGS));
203
204   /**
205    * GInetAddress:is-any:
206    *
207    * Whether this is the "any" address for its family.
208    * See g_inet_address_get_is_any().
209    *
210    * Since: 2.22
211    */
212   g_object_class_install_property (gobject_class, PROP_IS_ANY,
213                                    g_param_spec_boolean ("is-any",
214                                                          P_("Is any"),
215                                                          P_("Whether this is the \"any\" address for its family"),
216                                                          FALSE,
217                                                          G_PARAM_READABLE |
218                                                          G_PARAM_STATIC_STRINGS));
219
220   /**
221    * GInetAddress:is-link-local:
222    *
223    * Whether this is a link-local address.
224    * See g_inet_address_get_is_link_local().
225    *
226    * Since: 2.22
227    */
228   g_object_class_install_property (gobject_class, PROP_IS_LINK_LOCAL,
229                                    g_param_spec_boolean ("is-link-local",
230                                                          P_("Is link-local"),
231                                                          P_("Whether this is a link-local address"),
232                                                          FALSE,
233                                                          G_PARAM_READABLE |
234                                                          G_PARAM_STATIC_STRINGS));
235
236   /**
237    * GInetAddress:is-loopback:
238    *
239    * Whether this is the loopback address for its family.
240    * See g_inet_address_get_is_loopback().
241    *
242    * Since: 2.22
243    */
244   g_object_class_install_property (gobject_class, PROP_IS_LOOPBACK,
245                                    g_param_spec_boolean ("is-loopback",
246                                                          P_("Is loopback"),
247                                                          P_("Whether this is the loopback address for its family"),
248                                                          FALSE,
249                                                          G_PARAM_READABLE |
250                                                          G_PARAM_STATIC_STRINGS));
251
252   /**
253    * GInetAddress:is-site-local:
254    *
255    * Whether this is a site-local address.
256    * See g_inet_address_get_is_loopback().
257    *
258    * Since: 2.22
259    */
260   g_object_class_install_property (gobject_class, PROP_IS_SITE_LOCAL,
261                                    g_param_spec_boolean ("is-site-local",
262                                                          P_("Is site-local"),
263                                                          P_("Whether this is a site-local address"),
264                                                          FALSE,
265                                                          G_PARAM_READABLE |
266                                                          G_PARAM_STATIC_STRINGS));
267
268   /**
269    * GInetAddress:is-multicast:
270    *
271    * Whether this is a multicast address.
272    * See g_inet_address_get_is_multicast().
273    *
274    * Since: 2.22
275    */
276   g_object_class_install_property (gobject_class, PROP_IS_MULTICAST,
277                                    g_param_spec_boolean ("is-multicast",
278                                                          P_("Is multicast"),
279                                                          P_("Whether this is a multicast address"),
280                                                          FALSE,
281                                                          G_PARAM_READABLE |
282                                                          G_PARAM_STATIC_STRINGS));
283
284   /**
285    * GInetAddress:is-mc-global:
286    *
287    * Whether this is a global multicast address.
288    * See g_inet_address_get_is_mc_global().
289    *
290    * Since: 2.22
291    */
292   g_object_class_install_property (gobject_class, PROP_IS_MC_GLOBAL,
293                                    g_param_spec_boolean ("is-mc-global",
294                                                          P_("Is multicast global"),
295                                                          P_("Whether this is a global multicast address"),
296                                                          FALSE,
297                                                          G_PARAM_READABLE |
298                                                          G_PARAM_STATIC_STRINGS));
299
300
301   /**
302    * GInetAddress:is-mc-link-local:
303    *
304    * Whether this is a link-local multicast address.
305    * See g_inet_address_get_is_mc_link_local().
306    *
307    * Since: 2.22
308    */
309   g_object_class_install_property (gobject_class, PROP_IS_MC_LINK_LOCAL,
310                                    g_param_spec_boolean ("is-mc-link-local",
311                                                          P_("Is multicast link-local"),
312                                                          P_("Whether this is a link-local multicast address"),
313                                                          FALSE,
314                                                          G_PARAM_READABLE |
315                                                          G_PARAM_STATIC_STRINGS));
316
317   /**
318    * GInetAddress:is-mc-node-local:
319    *
320    * Whether this is a node-local multicast address.
321    * See g_inet_address_get_is_mc_node_local().
322    *
323    * Since: 2.22
324    */
325   g_object_class_install_property (gobject_class, PROP_IS_MC_NODE_LOCAL,
326                                    g_param_spec_boolean ("is-mc-node-local",
327                                                          P_("Is multicast node-local"),
328                                                          P_("Whether this is a node-local multicast address"),
329                                                          FALSE,
330                                                          G_PARAM_READABLE |
331                                                          G_PARAM_STATIC_STRINGS));
332
333   /**
334    * GInetAddress:is-mc-org-local:
335    *
336    * Whether this is an organization-local multicast address.
337    * See g_inet_address_get_is_mc_org_local().
338    *
339    * Since: 2.22
340    */
341   g_object_class_install_property (gobject_class, PROP_IS_MC_ORG_LOCAL,
342                                    g_param_spec_boolean ("is-mc-org-local",
343                                                          P_("Is multicast org-local"),
344                                                          P_("Whether this is an organization-local multicast address"),
345                                                          FALSE,
346                                                          G_PARAM_READABLE |
347                                                          G_PARAM_STATIC_STRINGS));
348
349   /**
350    * GInetAddress:is-mc-site-local:
351    *
352    * Whether this is a site-local multicast address.
353    * See g_inet_address_get_is_mc_site_local().
354    *
355    * Since: 2.22
356    */
357   g_object_class_install_property (gobject_class, PROP_IS_MC_SITE_LOCAL,
358                                    g_param_spec_boolean ("is-mc-site-local",
359                                                          P_("Is multicast site-local"),
360                                                          P_("Whether this is a site-local multicast address"),
361                                                          FALSE,
362                                                          G_PARAM_READABLE |
363                                                          G_PARAM_STATIC_STRINGS));
364 }
365
366 static void
367 g_inet_address_init (GInetAddress *address)
368 {
369   address->priv = g_inet_address_get_instance_private (address);
370 }
371
372 /**
373  * g_inet_address_new_from_string:
374  * @string: a string representation of an IP address
375  *
376  * Parses @string as an IP address and creates a new #GInetAddress.
377  *
378  * Returns: (nullable) (transfer full): a new #GInetAddress corresponding
379  * to @string, or %NULL if @string could not be parsed.
380  *     Free the returned object with g_object_unref().
381  *
382  * Since: 2.22
383  */
384 GInetAddress *
385 g_inet_address_new_from_string (const gchar *string)
386 {
387   struct in_addr in_addr;
388   struct in6_addr in6_addr;
389
390   g_return_val_if_fail (string != NULL, NULL);
391
392   /* If this GInetAddress is the first networking-related object to be
393    * created, then we won't have called g_networking_init() yet at
394    * this point.
395    */
396   g_networking_init ();
397
398   if (inet_pton (AF_INET, string, &in_addr) > 0)
399     return g_inet_address_new_from_bytes ((guint8 *)&in_addr, AF_INET);
400   else if (inet_pton (AF_INET6, string, &in6_addr) > 0)
401     return g_inet_address_new_from_bytes ((guint8 *)&in6_addr, AF_INET6);
402
403   return NULL;
404 }
405
406 #define G_INET_ADDRESS_FAMILY_IS_VALID(family) ((family) == AF_INET || (family) == AF_INET6)
407
408 /**
409  * g_inet_address_new_from_bytes:
410  * @bytes: (array) (element-type guint8): raw address data
411  * @family: the address family of @bytes
412  *
413  * Creates a new #GInetAddress from the given @family and @bytes.
414  * @bytes should be 4 bytes for %G_SOCKET_FAMILY_IPV4 and 16 bytes for
415  * %G_SOCKET_FAMILY_IPV6.
416  *
417  * Returns: a new #GInetAddress corresponding to @family and @bytes.
418  *     Free the returned object with g_object_unref().
419  *
420  * Since: 2.22
421  */
422 GInetAddress *
423 g_inet_address_new_from_bytes (const guint8         *bytes,
424                                GSocketFamily  family)
425 {
426   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
427
428   return g_object_new (G_TYPE_INET_ADDRESS,
429                        "family", family,
430                        "bytes", bytes,
431                        NULL);
432 }
433
434 /**
435  * g_inet_address_new_loopback:
436  * @family: the address family
437  *
438  * Creates a #GInetAddress for the loopback address for @family.
439  *
440  * Returns: a new #GInetAddress corresponding to the loopback address
441  * for @family.
442  *     Free the returned object with g_object_unref().
443  *
444  * Since: 2.22
445  */
446 GInetAddress *
447 g_inet_address_new_loopback (GSocketFamily family)
448 {
449   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
450
451   if (family == AF_INET)
452     {    
453       guint8 addr[4] = {127, 0, 0, 1};
454
455       return g_inet_address_new_from_bytes (addr, family);
456     }
457   else
458     return g_inet_address_new_from_bytes (in6addr_loopback.s6_addr, family);
459 }
460
461 /**
462  * g_inet_address_new_any:
463  * @family: the address family
464  *
465  * Creates a #GInetAddress for the "any" address (unassigned/"don't
466  * care") for @family.
467  *
468  * Returns: a new #GInetAddress corresponding to the "any" address
469  * for @family.
470  *     Free the returned object with g_object_unref().
471  *
472  * Since: 2.22
473  */
474 GInetAddress *
475 g_inet_address_new_any (GSocketFamily family)
476 {
477   g_return_val_if_fail (G_INET_ADDRESS_FAMILY_IS_VALID (family), NULL);
478
479   if (family == AF_INET)
480     {    
481       guint8 addr[4] = {0, 0, 0, 0};
482
483       return g_inet_address_new_from_bytes (addr, family);
484     }
485   else
486     return g_inet_address_new_from_bytes (in6addr_any.s6_addr, family);
487 }
488
489
490 /**
491  * g_inet_address_to_string:
492  * @address: a #GInetAddress
493  *
494  * Converts @address to string form.
495  *
496  * Returns: a representation of @address as a string, which should be
497  * freed after use.
498  *
499  * Since: 2.22
500  */
501 gchar *
502 g_inet_address_to_string (GInetAddress *address)
503 {
504   gchar buffer[INET6_ADDRSTRLEN];
505
506   g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
507
508   if (address->priv->family == AF_INET)
509     inet_ntop (AF_INET, &address->priv->addr.ipv4, buffer, sizeof (buffer));
510   else
511     inet_ntop (AF_INET6, &address->priv->addr.ipv6, buffer, sizeof (buffer));
512
513   return g_strdup (buffer);
514 }
515
516 /**
517  * g_inet_address_to_bytes: (skip)
518  * @address: a #GInetAddress
519  *
520  * Gets the raw binary address data from @address.
521  *
522  * Returns: a pointer to an internal array of the bytes in @address,
523  * which should not be modified, stored, or freed. The size of this
524  * array can be gotten with g_inet_address_get_native_size().
525  *
526  * Since: 2.22
527  */
528 const guint8 *
529 g_inet_address_to_bytes (GInetAddress *address)
530 {
531   g_return_val_if_fail (G_IS_INET_ADDRESS (address), NULL);
532
533   return (guint8 *)&address->priv->addr;
534 }
535
536 /**
537  * g_inet_address_get_native_size:
538  * @address: a #GInetAddress
539  *
540  * Gets the size of the native raw binary address for @address. This
541  * is the size of the data that you get from g_inet_address_to_bytes().
542  *
543  * Returns: the number of bytes used for the native version of @address.
544  *
545  * Since: 2.22
546  */
547 gsize
548 g_inet_address_get_native_size (GInetAddress *address)
549 {
550   if (address->priv->family == AF_INET)
551     return sizeof (address->priv->addr.ipv4);
552   return sizeof (address->priv->addr.ipv6);
553 }
554
555 /**
556  * g_inet_address_get_family:
557  * @address: a #GInetAddress
558  *
559  * Gets @address's family
560  *
561  * Returns: @address's family
562  *
563  * Since: 2.22
564  */
565 GSocketFamily
566 g_inet_address_get_family (GInetAddress *address)
567 {
568   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
569
570   return address->priv->family;
571 }
572
573 /**
574  * g_inet_address_get_is_any:
575  * @address: a #GInetAddress
576  *
577  * Tests whether @address is the "any" address for its family.
578  *
579  * Returns: %TRUE if @address is the "any" address for its family.
580  *
581  * Since: 2.22
582  */
583 gboolean
584 g_inet_address_get_is_any (GInetAddress *address)
585 {
586   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
587
588   if (address->priv->family == AF_INET)
589     {
590       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
591
592       return addr4 == INADDR_ANY;
593     }
594   else
595     return IN6_IS_ADDR_UNSPECIFIED (&address->priv->addr.ipv6);
596 }
597
598 /**
599  * g_inet_address_get_is_loopback:
600  * @address: a #GInetAddress
601  *
602  * Tests whether @address is the loopback address for its family.
603  *
604  * Returns: %TRUE if @address is the loopback address for its family.
605  *
606  * Since: 2.22
607  */
608 gboolean
609 g_inet_address_get_is_loopback (GInetAddress *address)
610 {
611   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
612
613   if (address->priv->family == AF_INET)
614     {
615       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
616
617       /* 127.0.0.0/8 */
618       return ((addr4 & 0xff000000) == 0x7f000000);
619     }
620   else
621     return IN6_IS_ADDR_LOOPBACK (&address->priv->addr.ipv6);
622 }
623
624 /**
625  * g_inet_address_get_is_link_local:
626  * @address: a #GInetAddress
627  *
628  * Tests whether @address is a link-local address (that is, if it
629  * identifies a host on a local network that is not connected to the
630  * Internet).
631  *
632  * Returns: %TRUE if @address is a link-local address.
633  *
634  * Since: 2.22
635  */
636 gboolean
637 g_inet_address_get_is_link_local (GInetAddress *address)
638 {
639   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
640
641   if (address->priv->family == AF_INET)
642     {
643       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
644
645       /* 169.254.0.0/16 */
646       return ((addr4 & 0xffff0000) == 0xa9fe0000);
647     }
648   else
649     return IN6_IS_ADDR_LINKLOCAL (&address->priv->addr.ipv6);
650 }
651
652 /**
653  * g_inet_address_get_is_site_local:
654  * @address: a #GInetAddress
655  *
656  * Tests whether @address is a site-local address such as 10.0.0.1
657  * (that is, the address identifies a host on a local network that can
658  * not be reached directly from the Internet, but which may have
659  * outgoing Internet connectivity via a NAT or firewall).
660  *
661  * Returns: %TRUE if @address is a site-local address.
662  *
663  * Since: 2.22
664  */
665 gboolean
666 g_inet_address_get_is_site_local (GInetAddress *address)
667 {
668   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
669
670   if (address->priv->family == AF_INET)
671     {
672       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
673
674       /* 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 */
675       return ((addr4 & 0xff000000) == 0x0a000000 ||
676               (addr4 & 0xfff00000) == 0xac100000 ||
677               (addr4 & 0xffff0000) == 0xc0a80000);
678     }
679   else
680     return IN6_IS_ADDR_SITELOCAL (&address->priv->addr.ipv6);
681 }
682
683 /**
684  * g_inet_address_get_is_multicast:
685  * @address: a #GInetAddress
686  *
687  * Tests whether @address is a multicast address.
688  *
689  * Returns: %TRUE if @address is a multicast address.
690  *
691  * Since: 2.22
692  */
693 gboolean
694 g_inet_address_get_is_multicast (GInetAddress *address)
695 {
696   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
697
698   if (address->priv->family == AF_INET)
699     {
700       guint32 addr4 = g_ntohl (address->priv->addr.ipv4.s_addr);
701
702       return IN_MULTICAST (addr4);
703     }
704   else
705     return IN6_IS_ADDR_MULTICAST (&address->priv->addr.ipv6);
706 }
707
708 /**
709  * g_inet_address_get_is_mc_global:
710  * @address: a #GInetAddress
711  *
712  * Tests whether @address is a global multicast address.
713  *
714  * Returns: %TRUE if @address is a global multicast address.
715  *
716  * Since: 2.22
717  */
718 gboolean
719 g_inet_address_get_is_mc_global (GInetAddress *address)
720 {
721   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
722
723   if (address->priv->family == AF_INET)
724     return FALSE;
725   else
726     return IN6_IS_ADDR_MC_GLOBAL (&address->priv->addr.ipv6);
727 }
728
729 /**
730  * g_inet_address_get_is_mc_link_local:
731  * @address: a #GInetAddress
732  *
733  * Tests whether @address is a link-local multicast address.
734  *
735  * Returns: %TRUE if @address is a link-local multicast address.
736  *
737  * Since: 2.22
738  */
739 gboolean
740 g_inet_address_get_is_mc_link_local (GInetAddress *address)
741 {
742   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
743
744   if (address->priv->family == AF_INET)
745     return FALSE;
746   else
747     return IN6_IS_ADDR_MC_LINKLOCAL (&address->priv->addr.ipv6);
748 }
749
750 /**
751  * g_inet_address_get_is_mc_node_local:
752  * @address: a #GInetAddress
753  *
754  * Tests whether @address is a node-local multicast address.
755  *
756  * Returns: %TRUE if @address is a node-local multicast address.
757  *
758  * Since: 2.22
759  */
760 gboolean
761 g_inet_address_get_is_mc_node_local (GInetAddress *address)
762 {
763   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
764
765   if (address->priv->family == AF_INET)
766     return FALSE;
767   else
768     return IN6_IS_ADDR_MC_NODELOCAL (&address->priv->addr.ipv6);
769 }
770
771 /**
772  * g_inet_address_get_is_mc_org_local:
773  * @address: a #GInetAddress
774  *
775  * Tests whether @address is an organization-local multicast address.
776  *
777  * Returns: %TRUE if @address is an organization-local multicast address.
778  *
779  * Since: 2.22
780  */
781 gboolean
782 g_inet_address_get_is_mc_org_local  (GInetAddress *address)
783 {
784   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
785
786   if (address->priv->family == AF_INET)
787     return FALSE;
788   else
789     return IN6_IS_ADDR_MC_ORGLOCAL (&address->priv->addr.ipv6);
790 }
791
792 /**
793  * g_inet_address_get_is_mc_site_local:
794  * @address: a #GInetAddress
795  *
796  * Tests whether @address is a site-local multicast address.
797  *
798  * Returns: %TRUE if @address is a site-local multicast address.
799  *
800  * Since: 2.22
801  */
802 gboolean
803 g_inet_address_get_is_mc_site_local (GInetAddress *address)
804 {
805   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
806
807   if (address->priv->family == AF_INET)
808     return FALSE;
809   else
810     return IN6_IS_ADDR_MC_SITELOCAL (&address->priv->addr.ipv6);
811 }
812
813 /**
814  * g_inet_address_equal:
815  * @address: A #GInetAddress.
816  * @other_address: Another #GInetAddress.
817  *
818  * Checks if two #GInetAddress instances are equal, e.g. the same address.
819  *
820  * Returns: %TRUE if @address and @other_address are equal, %FALSE otherwise.
821  *
822  * Since: 2.30
823  */
824 gboolean
825 g_inet_address_equal (GInetAddress *address,
826                       GInetAddress *other_address)
827 {
828   g_return_val_if_fail (G_IS_INET_ADDRESS (address), FALSE);
829   g_return_val_if_fail (G_IS_INET_ADDRESS (other_address), FALSE);
830
831   if (g_inet_address_get_family (address) != g_inet_address_get_family (other_address))
832     return FALSE;
833
834   if (memcmp (g_inet_address_to_bytes (address),
835               g_inet_address_to_bytes (other_address),
836               g_inet_address_get_native_size (address)) != 0)
837     return FALSE;
838
839   return TRUE;
840 }