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